2

我想编写一些测试来检查我使用 ForkJoinPool 对休息服务的异步调用。我有以下代码:

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();挂起。

这是我的线程转储(我不知道是否需要):

"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)

我正在使用 TestNg 和 Mockito

4

0 回答 0