Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/327.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
如何使用mockito和junit为Java中的ExecuterService编写测试用例?_Java_Multithreading_Junit_Mockito_Executorservice - Fatal编程技术网

如何使用mockito和junit为Java中的ExecuterService编写测试用例?

如何使用mockito和junit为Java中的ExecuterService编写测试用例?,java,multithreading,junit,mockito,executorservice,Java,Multithreading,Junit,Mockito,Executorservice,如何使用mockito和junit为ExecuterService编写测试用例 protected void updateRequest(String request) { ExecutorService executor = Executors.newFixedThreadPool(1); executor.submit(() -> { service.callGattuService(request); }); e

如何使用mockito和junit为ExecuterService编写测试用例

protected void updateRequest(String request) {

      ExecutorService executor = Executors.newFixedThreadPool(1);
      executor.submit(() -> {
          service.callGattuService(request);
      });

      executor.shutdown();
}
前面的方法不是多线程的,直接调用service.callGattuService(),下面是测试用例

@Test 
   public void updateGattu() throws SystemException, SocketException, IOException {
      when(service.callGattuService("11")).thenReturn(something); 

      testSubject.updateRequest(request)

      verify(service, times(1)).callGattuService("11");
   }

我不知道如何操作它,以便可以检测/调用ExecuterService

在这种情况下,最简单直接的解决方案是在验证之前添加延迟,而不更改测试逻辑,因为您已经在检查内部方法是否使用特定参数调用,无论是否通过executor调用

@Test 
public void updateGattu() throws SystemException, SocketException, IOException {
    testSubject.updateRequest("11")
    verify(service, after(2*1000).times(1)).callGattuService("11");
}
别忘了监视你的服务。希望有帮助

编辑: 在之前版本的mockito all(1.9.5)中,这里有一个类似的方法,可以这样使用

@Test 
public void updateGattu() throws SystemException, SocketException, IOException {
    testSubject.updateRequest("11")
    verify(service, timeout(2*1000)).callGattuService("11");
}

你能澄清一下你到底想用JUnit测试验证什么吗?现在,我认为您提到的测试将是成功的,无论执行器的使用情况如何。您想知道是否调用了executor服务还是什么?在实现Executer之后,它会创建另一个线程,结果不会在同一个线程中验证。是的,我想知道executer被调用了,可以找到after(),你能告诉我这是哪个包吗?这里你不在我的版本中。你使用哪个版本/工件?org.mockito mockito all 1.9.5