Spring asynch method integration test fails

安稳与你 提交于 2020-01-16 20:19:09

问题


I created an integration test for asynchronous rest controller method. Which looks like:

   @Test
    public void shouldHandleRequestsAsynchronously() throws Exception {
        MvcResult mvcResult = this.mockMvc.perform(get("/api/reports/daily?startDate=2004-04-13&endDate=2005-04-13"))
                .andExpect(request().asyncStarted())
                .andReturn();

        this.mockMvc.perform(asyncDispatch(mvcResult))
                .andExpect(status().isOk())
                .andExpect(jsonPath("$[0].totalDistance", equalTo(100)))
                .andExpect(jsonPath("$[0].totalPrice", equalTo(100.7)));
    }

The main problem is, that all the time I am getting the assertion error:

java.lang.AssertionError: Async started 
Expected :true
Actual   :false

At line with .andExpect(request().asyncStarted().To be honest I don't have any idea what is wrong.

My rest controller method is:

@GetMapping(value = "/daily")
public ResponseEntity<List<DailyReport>> getDailyReports(
        @PathParam("startDate") @DateTimeFormat(pattern = "YYYY-MM-DD") Date startDate,
        @PathParam("endDate") @DateTimeFormat(pattern = "YYYY-MM-DD") Date endDate) throws InterruptedException, ExecutionException {
    return new ResponseEntity<>(reportService.findReports(startDate, endDate).get(), HttpStatus.OK);
}

Do you have any idea what could be wrong?


回答1:


So,

I read documentation once again and I sort this problem out. If you want to use method request().asyncStarted(), you have to wrap your response Callable or DeferredResult.

  • Assert whether asynchronous processing started, usually as a result of a controller method returning {@link Callable} or {@link DeferredResult}.


来源:https://stackoverflow.com/questions/49593026/spring-asynch-method-integration-test-fails

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!