MockMvc failed waiting on async call

java.lang.IllegalStateException
Javadoc
Signals that a method has been invoked at an illegal or inappropriate time. In other words, the Java environment or Java application is not in an appropriate state for the requested operation. @author Jonni Kanerva @since JDK1.1
at org.springframework.test.web.servlet.DefaultMvcResult.getAsyncResult
(source)

Typical Exception Messages

  1. Async result for handler [public java.lang.String org.vpac.web.controller.DatasetController.createOrUpdateDataset(org.vpac.web.model.request.DatasetRequest,org.springframework.ui.ModelMap) throws org.vpac.web.exception.ResourceNotFoundException,java.io.IOException] was not set during the specified timeToWait=-1
  2. Gave up waiting on async result from handler [public org.springframework.web.context.request.async.DeferredResult<java.util.List<java.lang.String>> org.springframework.samples.async.chat.ChatController.getMessages(int)] to complete
  3. Gave up waiting on async result from handler [public org.springframework.web.context.request.async.DeferredResult<java.lang.String> SimpleController.handle()] to complete
  4. Async result for handler [rx.Single<java.util.List<MyType>> MyController.fetchFromDB()] was not set during the specified timeToWait=-1
  5. Async result for handler [public org.springframework.web.context.request.async.DeferredResult<org.springframework.http.ResponseEntity<org.springframework.core.io.InputStreamResource>> com.example.controller.FileController.getSomeFile()] was not set during the specified timeToWait=9223372036854775807
  6. Async result for handler [public org.springframework.web.context.request.async.DeferredResult<java.lang.String> my.package.TasksController.testit()] was not set during the specified timeToWait=1500

Specific cases

Solution

JaviRpo, 3 months ago

This error happens when the test does the assert before the async result is complete.

You do need to get the asyncResult before calling performing an async dispatcher.

External results for this pattern (4)

  1. Kenstervia Stack Overflow3 months ago
    Async result for handler [public org.springframework.web.context.request.async.DeferredResult<java.lang.String> my.package.TasksController.testit()] was not set during the specified timeToWait=1500
    Show stack trace
  2. piotrekvia Stack Overflow5 months ago
    Async result for handler [rx.Single<java.util.List<MyType>> MyController.fetchFromDB()] was not set during the specified timeToWait=-1
    Show stack trace
  3. marknorkinvia Stack Overflow5 months ago
    Async result for handler [public org.springframework.web.context.request.async.DeferredResult<org.springframework.http.ResponseEntity<org.springframework.core.io.InputStreamResource>> com.example.controller.FileController.getSomeFile()] was not set during the specified timeToWait=9223372036854775807
    Show stack trace
  4. Maxim Valyanskiyvia Spring JIRA5 months ago
    Gave up waiting on async result from handler [public org.springframework.web.context.request.async.DeferredResult<java.lang.String> SimpleController.handle()] to complete
    Show stack trace