2

我有这个问题:

@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);
       }
    });
}

如何让这些异常无法通过我的测试?

4

1 回答 1

4

submit()方法返回一个Future<?>. 如果您尝试获取 Future 的结果,该方法将抛出已被抛出的 Throwable Runnable

Future<?> future = stpe.submit( .... 

future.get(); // this call will throw the exception that has been thrown in the Runnable.

这样,run 方法中的异常/错误将使测试失败。

于 2013-03-02T02:16:49.670 回答