title: "AssertionError: Async not started" when trying to test a controller which invokes an @Async method
body: <p>I have a similar problem to <a href="https://stackoverflow.com/questions/49593026/spring-asynch-method-integration-test-fails">this one</a>: when trying to make an integration test on a controller which invokes an <code>@Async</code> method from a service, an <code>AssertionError: Async not started</code> occurs.</p>
  <p>The guy suggests something that I don't clearly understand:</p>
  <blockquote>
  <p>you have to wrap your response Callable or DeferredResult</p>
  </blockquote>
  <p>That's my service method:</p>
  <pre><code>@Async
  @Override
  public Future&lt;String&gt; migrateMovies() {
      Collection&lt;MetadataItem&gt; metadataMovies = metadataItemService.getAllByMetadataType(MOVIE_TYPE);
  
      Collection&lt;Movie&gt; movies = mapMovies(metadataMovies);
      movieService.saveMovies(movies);
      return CompletableFuture.supplyAsync(() -&gt; movies.size() + &quot; movies migrated successfully!&quot;);
  }
  </code></pre>
  <p>That's my controller method:</p>
  <pre><code>@PostMapping(&quot;/movies&quot;)
  public ResponseEntity&lt;String&gt; migrateMovies() throws ExecutionException, InterruptedException {
      return ResponseEntity.ok(movieMigrationService.migrateMovies().get());
  }
  </code></pre>
  <p>And that's how I'm trying to test it:</p>
  <pre><code>@Test
  @WithMockUser(username = &quot;username&quot;)
  void migrateMoviesSuccessfully() throws Exception {
      Collection&lt;Movie&gt; expectedMovies = Lists.list(new Movie(), new Movie());
      when(movieService.saveMovies(anyCollection())).thenReturn(expectedMovies);
  
      Future&lt;String&gt; expectedResponse =
              CompletableFuture.supplyAsync(() -&gt; expectedMovies.size() + &quot; movies migrated successfully!&quot;);
      when(movieMigrationService.migrateMovies()).thenReturn(expectedResponse);
  
      MvcResult mvcResult = mockMvc.perform(post(&quot;/movies&quot;))
              .andExpect(request().asyncStarted())
              .andReturn();
  
      mockMvc.perform(asyncDispatch(mvcResult))
              .andExpect(status().isOk())
              .andExpect(content().string(&quot;2 movies migrated successfully!&quot;));
  }
  </code></pre>
  <p>Could someone explain concretely for my example how this wrapping should happen?</p>
  <p>I'm open to any other suggestions too!</p>
  <p><strong>NOTE:</strong> My application is properly running asynchronously + my unit tests for the service <code>migrateMovies()</code> method are successful, it's only the integration test that is being stubborn :)</p>