Testing Spring asyncResult() and jsonPath() together

后端 未结 3 781
情话喂你
情话喂你 2021-02-13 09:19

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:40

    Bud's answer really helped point me in the right direction however it didn't quite work because it did not wait for the async result. Since posting this question, the spring-mvc-showcase samples (https://github.com/SpringSource/spring-mvc-showcase) have been updated.

    It seems like in the first part of the call when you retrieve the MvcResult, you need to assert on an asyncResult() and in the case of JSON pojo mapping you need to assert on the actual type itself (not JSON). So I needed to add the third line below to Bud's answer, then the rest just works.

    MvcResult mvcResult = this.mockMvc.perform(get("/trigger/job/xyz"))
        .andExpect(request().asyncStarted())
        .andExpect(request().asyncResult(instanceOf(TriggerResult.class)))
        .andReturn();
    
    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"));
    

    Note: instanceOf() is org.hamcrest.CoreMatchers.instanceOf. To get access to Hamcrest libraries include the latest hamcrest-library jar.

    For maven ...

        
            org.hamcrest
            hamcrest-library
            LATEST VERSION HERE
            test
        
    

提交回复
热议问题