title: JUnit-testing a Spring @Async void service method
body: <p>I have a Spring service:</p>
  
  <pre><code>@Service
  @Transactional
  public class SomeService {
  
      @Async
      public void asyncMethod(Foo foo) {
          // processing takes significant time
      }
  }
  </code></pre>
  
  <p>And I have an integration test for this <code>SomeService</code>:</p>
  
  <pre><code>@RunWith(SpringJUnit4ClassRunner.class)
  @SpringApplicationConfiguration(classes = Application.class)
  @WebAppConfiguration
  @IntegrationTest
  @Transactional
  public class SomeServiceIntTest {
  
      @Inject
      private SomeService someService;
  
          @Test
          public void testAsyncMethod() {
  
              Foo testData = prepareTestData();
  
              someService.asyncMethod(testData);
  
              verifyResults();
          }
  
          // verifyResult() with assertions, etc.
  }
  </code></pre>
  
  <p>Here is the problem:</p>
  
  <ul>
  <li>as <code>SomeService.asyncMethod(..)</code> is annotated with <code>@Async</code> and</li>
  <li>as the <code>SpringJUnit4ClassRunner</code> adheres to the <code>@Async</code> semantics</li>
  </ul>
  
  <p>the <code>testAsyncMethod</code> thread will fork the call <code>someService.asyncMethod(testData)</code> into its own worker thread, then directly continue executing <code>verifyResults()</code>, possibly before the previous worker thread has finished its work.</p>
  
  <p>How can I wait for <code>someService.asyncMethod(testData)</code>'s completion before verifying the results? Notice that the solutions to <em><a href="https://stackoverflow.com/q/20807232/923560">How do I write a unit test to verify async behavior using Spring 4 and annotations?</a></em> don't apply here, as <code>someService.asyncMethod(testData)</code> returns <code>void</code>, not a <code>Future&lt;?&gt;</code>.</p>