我有这个问题:
@Test
public void foo() throws Exception {
ScheduledThreadPoolExecutor stpe = new ScheduledThreadPoolExecutor(1);
stpe.submit(new Runnable() {
@Override
public void run() {
// does not make this unit test fail :(
Assert.AssertEquals(1, 2);
}
});
}
如何让这些例外导致我的测试失败?
答案 0 :(得分:4)
submit()
方法返回Future<?>
。如果你试图获得Future的结果,该方法将抛出抛出Runnable
内的Throwable:
Future<?> future = stpe.submit( ....
future.get(); // this call will throw the exception that has been thrown in the Runnable.
这样,run方法中的异常/错误将使测试失败。