Java 使用testng测试forkjoinpool
我想编写一些测试,用ForkJoinPool检查对rest服务的异步调用。我有以下代码:Java 使用testng测试forkjoinpool,java,unit-testing,testing,java.util.concurrent,forkjoinpool,Java,Unit Testing,Testing,Java.util.concurrent,Forkjoinpool,我想编写一些测试,用ForkJoinPool检查对rest服务的异步调用。我有以下代码: final ForkJoinTask<List<Something>> someJoinTask1 = new RecursiveTaskLambda<>( () -> service1.findAll(filter)); this.forkJoinPool.execute(someJoinTask); final ForkJoinT
final ForkJoinTask<List<Something>> someJoinTask1 = new RecursiveTaskLambda<>(
() -> service1.findAll(filter));
this.forkJoinPool.execute(someJoinTask);
final ForkJoinTask<List<Something>> someJoinTask2 = new RecursiveTaskLambda<>(
() -> service2.findAll(filter));
this.forkJoinPool.execute(someJoinTask);
// some stuff happening here
try {
firstList= someJoinTask1.get();
secondList = someJoinTask2.get();
}
catch (final InterruptedException | ExecutionException e) {
throw new MyException("Couldn't load mandatory data for screen", e);
}
getView().setSomething1(firstList);
getView().setSomething2(secondList);
//prepare some stuff for the test
Mockito.when(this.service1.findAll(Matchers.any(MyFilter.class))).thenReturn(someStuff);
// ----Call tested method----
someMethod();
Mockito.verify(this.view).setSomething1(captor.capture());
Mockito.verify(this.view).setSomething2(captor.capture());
// ----Asserts----
问题是,当我执行测试时,它会永远运行。我试图添加Thread.sleep()只是想看看这是否会改变,但没有改变。当我尝试调试测试时,它会通过所有准备代码调用测试方法,当它到达firstList=someJoinTask1.get()时代码>在someMethod()中代码>它挂起
这是我的线程转储(我不知道是否需要):
我正在使用TestNg和Mockito
"main@1" prio=5 tid=0x1 nid=NA waiting
java.lang.Thread.State: WAITING
at java.lang.Object.wait(Object.java:-1)
at java.util.concurrent.ForkJoinTask.externalInterruptibleAwaitDone(ForkJoinTask.java:367)
at java.util.concurrent.ForkJoinTask.get(ForkJoinTask.java:1001)
at someMethod(the first get line)