Spring 异步方法集成测试失败
Spring asynch method integration test fails
我为异步休息控制器方法创建了一个集成测试。看起来像:
@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)));
}
主要问题是,我一直收到断言错误:
java.lang.AssertionError: Async started
Expected :true
Actual :false
与 .andExpect(request().asyncStarted()
一致。老实说,我不知道哪里出了问题。
我的休息控制器方法是:
@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);
}
你知道哪里出了问题吗?
所以,
我再次阅读文档并解决了这个问题。如果您想使用方法 request().asyncStarted()
,您必须将您的响应包裹起来 Callable
或 DeferredResult
。
- Assert whether asynchronous processing started, usually as a
result of a controller method returning {@link Callable} or {@link
DeferredResult}.
我为异步休息控制器方法创建了一个集成测试。看起来像:
@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)));
}
主要问题是,我一直收到断言错误:
java.lang.AssertionError: Async started
Expected :true
Actual :false
与 .andExpect(request().asyncStarted()
一致。老实说,我不知道哪里出了问题。
我的休息控制器方法是:
@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);
}
你知道哪里出了问题吗?
所以,
我再次阅读文档并解决了这个问题。如果您想使用方法 request().asyncStarted()
,您必须将您的响应包裹起来 Callable
或 DeferredResult
。
- Assert whether asynchronous processing started, usually as a result of a controller method returning {@link Callable} or {@link DeferredResult}.