Testing Spring asyncResult() and jsonPath() together

后端 未结 3 1357
走了就别回头了
走了就别回头了 2021-02-13 09:08

I\'m using a restful url to kick off a long-running backend process (it is normally on a cron schedule, but we want the ability to kick it off manually).

The code below

3条回答
  •  逝去的感伤
    2021-02-13 09:54

    I think you want to use asyncDispatch on the result of the started Async calls Reference code from link below

    http://static.springsource.org/spring/docs/3.2.x/javadoc-api/org/springframework/test/web/servlet/request/MockMvcRequestBuilders.html

    Usage involves performing one request first that starts async processing:

     MvcResult mvcResult = this.mockMvc.perform(get("/trigger/job/xyz"))
            .andExpect(request().asyncStarted())
            .andReturn();
    

    And then performing the async dispatch re-using the MvcResult:

     this.mockMvc.perform(asyncDispatch(mvcResult))
            .andExpect(status().isOk())
            .andExpect(content().contentType(MediaType.APPLICATION_JSON))
            .andExpect(content().string(.......));
    

    or in your case

    this.mockMvc.perform(asyncDispatch(mvcResult))
            .andExpect(status().isOk())
            .andExpect(content().contentType(MediaType.APPLICATION_JSON))
            .andExpect(jsonPath("status").value("SUCCESS"))
            .andExpect(jsonPath("message").value("A meaningful message appears"));
    

提交回复
热议问题