【发布时间】:2020-05-26 14:49:47
【问题描述】:
控制器
@PostMapping("/endpoint/group/move")
public DeferredResult<ResponseEntity<List<Endpoint>>> moveEndpointGroup(@RequestBody List<String> EndpointIds,
@RequestParam("from") String fromGroupId, @RequestParam("to") String toGroupId)
throws ResourceNotFoundException, ExecutionException {
DeferredResult<ResponseEntity<List<Endpoint>>> result = new DeferredResult<>();
if (EndpointIds != null && !EndpointIds.isEmpty()) {
CompletableFuture.supplyAsync(() -> EndpointService.moveEndpoints(EndpointIds, fromGroupId, toGroupId), executor)
.whenComplete((movedEndpointsList, ex) -> {
if (ex != null) {
throw new RuntimeException(ex);
}
if (movedEndpointsList.size() == EndpointIds.size()) {
result.setResult(ResponseEntity.status(HttpStatus.OK).body(movedEndpointsList));
} else {
result.setResult(ResponseEntity.status(HttpStatus.PARTIAL_CONTENT).body(movedEndpointsList));
}
});
}
return result;
}
我已经写了以下测试方法,它不起作用。当我运行此测试时,我得到处理程序的异步结果未在指定的 timeToWait 期间设置。
有人可以帮我解决我做错的地方吗?
@Test
public void testMoveEndpointGroup_WhenSuccess() throws Exception {
List<Endpoint> EndpointList = Arrays.asList(Endpoint, Endpoint);
List<String> EndpointIds = Arrays.asList("123");
Mockito.when(EndpointService.moveEndpoints(Mockito.any(), Mockito.anyString(), Mockito.anyString()))
.thenReturn(EndpointList);
RequestBuilder requestBuilder = MockMvcRequestBuilders.post("/endpoint/group/move")
.contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(EndpointIds)).param("from", "gorupA").param("to", "groupB");
MvcResult result = mvc.perform(requestBuilder).andReturn();
result = mvc.perform(MockMvcRequestBuilders.asyncDispatch(result)).andDo(MockMvcResultHandlers.print()).andReturn();
JsonNode actual = objectMapper.readTree(result.getResponse().getContentAsString()).get("content");
String expected = objectMapper.writeValueAsString(Arrays.asList(EndpointList));
Assert.assertEquals(expected, actual.toString());
}
【问题讨论】:
标签: java spring-mvc junit mockito spring-boot-test