title: JUnit testing an asynchronous method with Mockito
body: <p>I have implemented an asynchronous method in a Java 1.8 class using Spring Framework (version 5.0.5.RELEASE):</p>
  
  <pre><code>public class ClassToBeTested {
      @Autowired
      private MyComponent myComponent;
  
      @Async
      public void doStuff(List&lt;MyClass&gt; myObjects) {
          CompletableFuture&lt;MyResponseObject&gt;[] futureList = new CompletableFuture[myObjects.size()];
          int count = 0;
  
          for (MyClass myObject : myObjects) {
              futureList[count] = myComponent.doOtherStuff(myObject);
              count++;
          }
  
          // Wait until all doOtherStuff() calls have been completed
          CompletableFuture.allOf(futureList).join();
  
          ... other stuff ...
      }
  }
  </code></pre>
  
  <p>I'm trying to test the class with JUnit and Mockito. I have set it up as follows, with an aim of mocking the doStuff() method's call to the component:</p>
  
  <pre><code>@MockBean
  private MyComponent myComponentAsAMock;
  
  @InjectMocks
  @Autowired
  private ClassToBeTested classToBeTested;
  
  @Test
  public void myTest() throws Exception {
      // Create object to return when myComponent.doOtherStuff() is called.
      CompletableFuture&lt;MyResponseObject&gt; completableFuture = new CompletableFuture&lt;MyResponseObject&gt;();
      ... populate an instance of MyResponseObject ...
      completableFuture.complete(myResponseObject);
  
      // Return object when myComponent.doOtherStuff() is called.
      Mockito.when(
          myComponentAsAMock.doOtherStuff(ArgumentMatchers.any(MyClass.class)))
          .thenReturn(completableFuture);
  
      // Test.
      List&lt;MyClass&gt; myObjects = new ArrayList&lt;MyClass&gt;();
      MyClass myObject = new MyClass();
      myobjects.add(myObject);
      classToBeTested.doStuff(myObjects);
  }
  </code></pre>
  
  <p>Whilst the unit test seems to be successful when I run it individually in Eclipse,  but doing a Maven build of the whole project I notice NullPointerExceptions are being thrown:</p>
  
  <pre><code>[ThreadExecutor2] .a.i.SimpleAsyncUncaughtExceptionHandler : Unexpected error occurred invoking async method 'public void package.ClassToBeTested.doStuff(java.util.List)'.
  
  java.lang.NullPointerException: null
  at java.util.concurrent.CompletableFuture.andTree(CompletableFuture.java:1306) ~[na:1.8.0_131]
  at java.util.concurrent.CompletableFuture.allOf(CompletableFuture.java:2225) ~[na:1.8.0_131]
  at package.ClassToBeTested.doStuff(ClassToBeTested.java:75) ~[classes/:na]
  </code></pre>
  
  <p>The error is being raised on this line of ClassToBeTested.java:</p>
  
  <pre><code>CompletableFuture.allOf(completedFutureList).join();
  </code></pre>
  
  <p>It <em>looks</em> like the exception message is being displayed in the Maven build output after the test has finished (there are other tests being run whose output occur before the error message is being displayed), so I'm guessing it's something to do with the fact that the call to doStuff() is asynchronous.</p>
  
  <p>Any assistance would be appreciated.</p>