Java 在TestNG中测试多线程代码
我有一个问题:Java 在TestNG中测试多线程代码,java,multithreading,testng,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
@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);
}
});
}
如何使这些异常通过测试?方法submit()
返回一个Future
。如果您试图获得将来的结果,则该方法将抛出在可运行
中抛出的Throwable:
Future<?> future = stpe.submit( ....
future.get(); // this call will throw the exception that has been thrown in the Runnable.
Future-Future=stpe.submit(。。。。
future.get();//此调用将引发Runnable中引发的异常。
这样,run方法中的异常/错误将使测试失败。submit()方法返回一个Future
。如果您试图获得Future的结果,该方法将抛出在Runnable
中抛出的Throwable:
Future<?> future = stpe.submit( ....
future.get(); // this call will throw the exception that has been thrown in the Runnable.
Future-Future=stpe.submit(。。。。
future.get();//此调用将引发Runnable中引发的异常。
这样,run方法中的异常/错误将导致测试失败。这是正确的,但并不能解决我的实际情况,即我没有控制线程池,我不正确地提取了它。所以我将接受这个并发布另一个?你的意思是你没有控制调用submit方法的代码,所以你无法获得未来的对象?我我猜另一个有更多细节的问题会很好,是的。这里-这是正确的,但不能解决我不控制线程池的实际情况,我不正确地提取了它。所以我会接受这个问题并发布另一个?你的意思是你不控制调用submit方法的代码,所以你无法获得未来的对象?我猜不是她的问题再详细一点就好了,是的,在这里-