Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在TestNG中测试多线程代码_Java_Multithreading_Testng - Fatal编程技术网

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方法的代码,所以你无法获得未来的对象?我猜不是她的问题再详细一点就好了,是的,在这里-