2

This my test case:

@Test
public void sampleTestCase(int parm1, int parm2, Integer expectedReturnedMessageCode, String expectedReturnedparm2, Integer expectedReturnedparm1) throws Exception {
        MvcResult result = mockMvc
                .perform( MockMvcRequestBuilders
                        .post("/sampleapi")
                        .content(asJsonString(new SampleRequestClass(parm1, parm2)))
                        .contentType(MediaType.APPLICATION_JSON)
                        .accept(MediaType.APPLICATION_JSON))
                .andDo(print())
                .andReturn();

        mockMvc.perform(asyncDispatch(result))
                .andExpect(status().isOk())
                .andDo(print())
                .andExpect(jsonPath("message_code").value(expectedReturnedMessageCode))
                .andExpect(jsonPath("data.parm1").value(expectedReturnedparm1))
                .andExpect(jsonPath("data.parm2").value(expectedReturnedparm2));
    }

This "sampleapi" returns a DeferredResult object. When I run this test case I'm getting the following error.

java.lang.IllegalStateException: The asyncDispatch CountDownLatch was not set by the TestDispatcherServlet.

    at org.springframework.util.Assert.state(Assert.java:73)
    at org.springframework.test.web.servlet.DefaultMvcResult.awaitAsyncDispatch(DefaultMvcResult.java:158)
    at org.springframework.test.web.servlet.DefaultMvcResult.getAsyncResult(DefaultMvcResult.java:145)
    at org.springframework.test.web.servlet.DefaultMvcResult.getAsyncResult(DefaultMvcResult.java:136)
    at org.springframework.test.web.servlet.request.MockMvcRequestBuilders.asyncDispatch(MockMvcRequestBuilders.java:269)

I'm relatively new to springboot and earlier this unit case was written using @SpringBootTest and this case runs successfully, later I found that I can run test cases without actually loading everything so i switched to @WebMvcTest. I fixed all other dependencies (@MockBean). Now i really stuck at this. My understanding about this issue is the Async is not started when using mockmvc. if anybody can tell how to fix it that would be great, because I'm stuck.

Saed Manaf
  • 41
  • 5

0 Answers0