在TestNG中测试多线程代码

时间:2013-03-02 02:08:07

标签: java multithreading testng

我有这个问题:

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

如何让这些例外导致我的测试失败?

1 个答案:

答案 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方法中的异常/错误将使测试失败。