My POST request is Asynchronous because I am using spring-data-rest. Now I am having issue when trying to write a test case for the POST request. The exception is "Async started expected: true but was: false"
I have read many articles and they all say how to do a test case for Asynchronous GET method but not POST method. I am trying to implement the logic suggested in these article for the GET method but receiving an exception.
private final MediaType contentType = new
MediaType(MediaType.APPLICATION_JSON.getType(),
MediaType.APPLICATION_JSON.getSubtype(), Charset.forName("utf8"));
private MockMvc mockMvc;
@Autowired
private WebApplicationContext webApplicationContext;
@Test
public void addProgramToTestDefaultValuesUsingAsync() throws Exception {
UserProgram request = getProgramRequest();
MvcResult result = mockMvc.perform(post("/programs")
.content(TestUtils.asJsonString(request))
.contentType(contentType))
.andDo(print())
.andExpect(request().asyncStarted())
.andReturn();
mockMvc.perform(asyncDispatch(result))
.andExpect(status().isCreated())
.andExpect(header().string(CONTENT_TYPE, APPLICATION_JSON_VALUE))
.andExpect(jsonPath("programRetries").value("3"));
}
I am expecting it to wait until the call is finished and then want to check the response against excepted value. But the call is failing at request().asyncStarted() and it returning "Async started expected: true but was: false". I have read many articles but nothing addresses this issue. Any help is appreciated.
For building my test case I have used the following articles: