[英]Test multithreaded code in TestNG
I have this problem: 我有这个问题:
@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);
}
});
}
How do I get those exceptions to fail my test? 如何让这些例外无法通过测试?
The submit()
method returns a Future<?>
. submit()
方法返回Future<?>
。 If you try to get the result of the Future, the method will throw the Throwable that has been thrown inside the Runnable
: 如果你试图得到Future的结果,该方法将抛出抛出Runnable
内的Throwable:
Future<?> future = stpe.submit( ....
future.get(); // this call will throw the exception that has been thrown in the Runnable.
This way, exceptions/errors in the run method will make the test fail. 这样,run方法中的异常/错误将使测试失败。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.