6

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:

https://niels.nu/blog/2016/spring-async-rest.html

http://callistaenterprise.se/blogg/teknik/2014/06/23/testing-non-blocking-rest-services-with-spring-mvc-and-spring-boot/

anand
  • 166
  • 1
  • 10
  • I have tried creating "testing" for async tasks in my project yet but from my point of view, you should expect Callable or CompletableFuture type response from your Controller method, right? Besides Ice had already solved this issue link: https://stackoverflow.com/a/49593498/8814467 if this helps let me know good luck. @anand – Aliy May 23 '19 at 12:09

1 Answers1

0

I have the same problem. Try following and see if it works for you, if you are trying to test a controller api:

mockMvc = MockMvcBuilders.standaloneSetup(new UserApi(new ServiceUser()).setAsyncRequestTimeout(1000).build();

Hope it helps.

Azhar Khan
  • 3,829
  • 11
  • 26
  • 32
  • 1
    https://stackoverflow.com/editing-help and also helpful in general: [tour] , [answer] – Yunnosch Jul 29 '22 at 10:00
  • 1
    I recommend against rhetoric questions in answers. They risk being misunderstood as not an answer at all. You are trying to answer the question at the top of this page, aren't you? Otherwise please delete this post. – Yunnosch Jul 29 '22 at 10:02
  • Please phrase this as an explained conditional answer, in order to avoid the impression of asking a clarification question instead of answering (for which a comment should be used instead of an answer, compare https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead ). For example like "If your problem is ... then the solution is to .... because .... ." – Yunnosch Jul 29 '22 at 10:02
  • 1
    I also recommend to avoid phrasings like "I have the same problem.", they increase the risk of being mistaken for a question instead of an answer, but I am convinced that there is an answer here between that and and "?"s. – Yunnosch Jul 29 '22 at 10:04