【发布时间】:2015-03-30 22:45:08
【问题描述】:
我正在寻找一种方法来捕获 JUnit 测试抛出的所有异常,然后重新抛出它们;在异常发生时向有关测试状态的错误消息添加更多详细信息。
JUnit 捕获 org.junit.runners.ParentRunner 中抛出的错误
protected final void runLeaf(Statement statement, Description description,
RunNotifier notifier) {
EachTestNotifier eachNotifier = new EachTestNotifier(notifier, description);
eachNotifier.fireTestStarted();
try {
statement.evaluate();
} catch (AssumptionViolatedException e) {
eachNotifier.addFailedAssumption(e);
} catch (Throwable e) {
eachNotifier.addFailure(e);
} finally {
eachNotifier.fireTestFinished();
}
}
不幸的是,这个方法是最终的,所以它不能被覆盖。此外,当异常被捕获时,像 Thread.UncaughtExceptionHandler 这样的东西也无济于事。我能想到的唯一其他解决方案是每个测试周围的 try/catch 块,但该解决方案不是很容易维护。谁能指出一个更好的解决方案?
【问题讨论】:
标签: junit