我正在使用SpringBoot 2.0.6-RELEASE
当我将我的api配置为使用不提供执行服务的CompletableFuture时-我的测试成功运行。例如,此测试通过了
public Future<List<AlertsBodyVO>> getAlerts(@PathVariable(value = "userId") final String userId) {
return CompletableFuture.supplyAsync(() -> Service.getAlerts(userId));
}
@Test
public void testGetAlerts_NullResult() throws Exception {
String userId = "a382839";
List<AlertsBodyVO> alertsVOList = null;
Mockito.when(mockService.getAlerts(Mockito.anyString())).thenReturn(alertsVOList);
MvcResult result = this.mockMvc.perform(get("/alerts/{userId}", userId)).andExpect(status().isOk())
.andReturn();
mockMvc.perform(asyncDispatch(result)).andExpect(status().isOk())
.andExpect(jsonPath("$").doesNotExist())
.andReturn();
Mockito.verify(mockService, Mockito.times(1)).getAlerts(Mockito.anyString());
}
但是-当我定义我的api以如下使用executorService
public Future<List<AlertsBodyVO>> getAlerts(@PathVariable(value = "userId") final String userId) {
return CompletableFuture.supplyAsync(() -> Service.getAlerts(userId), executorService);
}
我收到以下错误
java.lang.IllegalStateException: Async result for handler [public java.util.concurrent.Future<java.util.List<com.domain.AlertsBodyVO>> com.controllers.API.getAlerts(java.lang.String)] was not set during the specified timeToWait=10000
at org.springframework.test.web.servlet.DefaultMvcResult.getAsyncResult(DefaultMvcResult.java:146)
at org.springframework.test.web.servlet.DefaultMvcResult.getAsyncResult(DefaultMvcResult.java:136)
at org.springframework.test.web.servlet.request.MockMvcRequestBuilders.asyncDispatch(MockMvcRequestBuilders.java:269)
at com.controllers.APITest.testGetAlerts_NullResult(APITest.java:207)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.mockito.internal.runners.DefaultInternalRunner$1.run(DefaultInternalRunner.java:78)
at org.mockito.internal.runners.DefaultInternalRunner.run(DefaultInternalRunner.java:84)
at org.mockito.internal.runners.StrictRunner.run(StrictRunner.java:39)
at org.mockito.junit.MockitoJUnitRunner.run(MockitoJUnitRunner.java:161)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:89)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:41)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:541)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:763)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:463)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:209)
有什么想法,当我指定要使用的executorService时如何使我的MockMvc测试通过?
答案 0 :(得分:1)
使用模拟的ExecuterService
很难,因为您还应该嘲笑它们的所有行为,例如
Mockito.when(mockedExecuterService.execute...
执行器服务内部还有另一个隐藏的调用和过程。
取而代之的是,您可以仅出于测试目的创建简单的单线程执行程序,我检查了一下它是否可以与真实的ExecutorService
(例如Executors.newSingleThreadExecutor()
)一起正常工作,并且无法进行模拟。我想您现在如何创建测试上下文-Spring Test Context