【问题标题】:Spring asynch method integration test failsSpring异步方法集成测试失败
【发布时间】:2018-09-10 14:14:00
【问题描述】:

我为异步休息控制器方法创建了一个集成测试。看起来像:

   @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);
}

您知道可能出了什么问题吗?

【问题讨论】:

    标签: spring-boot spring-test spring-restcontroller spring-rest


    【解决方案1】:

    所以,

    我再次阅读文档并解决了这个问题。如果你想使用方法request().asyncStarted(),你必须把你的回复包装起来CallableDeferredResult

    • 断言异步处理是否启动,通常作为 返回 {@link Callable} 或 {@link 的控制器方法的结果 DeferredResult}。

    【讨论】:

      猜你喜欢
      • 2019-12-26
      • 1970-01-01
      • 2017-07-02
      • 2016-06-13
      • 2020-03-18
      • 2021-03-20
      • 1970-01-01
      • 2017-02-07
      • 1970-01-01
      相关资源
      最近更新 更多