我为异步休息控制器方法创建了一个集成测试。看起来像:
@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);
}
您知道可能出了什么问题吗?
So,
我再次阅读文档并解决了这个问题。如果你想使用方法request().asyncStarted()
,你必须包装你的回复Callable
or DeferredResult
.
- 断言异步处理是否开始,通常作为
控制器方法返回 {@link Callable} 或 {@link
延迟结果}。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)