title: Unit test of method with CompletableFuture inside
body: <p>I have method which in async way calls <code>connector.runSomeService(data)</code> and handles the response in method <code>handleServiceResponse(res, node)</code>. </p>
  
  <pre><code>public void runServiceOnAllNodes(Collection&lt;Node&gt; nodes, Object data) {
      nodes.parallelStream().forEach(node -&gt; {
          CompletableFuture&lt;ResponseEntity&gt; response = CompletableFuture
                  .supplyAsync(()-&gt; connector.runSomeService(data));
          response.exceptionally(ex -&gt; {
                      log.error("OMG...OMG!!!")
                      return null;
                  })
                  .thenAcceptAsync(res -&gt; handleServiceResponse(res, node));
      });
  }
  
  
  private void handleServiceResponse(ResponseEntity res, Node node) {
      if (res.isOK) {
          node.setOKStatus();
      } else {
          node.setFailStatus();
      }
      dbService.saveNode(node);
  }
  </code></pre>
  
  <p>Try to create unit test but when I try to verify if response is properly handled, the result of UT is non deterministic. </p>
  
  <pre><code>@Test
  public void testRunServiceOnAllNodes() {
      // given
      List&lt;Collector&gt; nodes = Arrays.asList(node1, node2, node3);
      when(connector.runSomeService(eq(node1), eq(data))).thenReturn(ResponseEntity.ok().body("{message:OK}"));
      when(connector.runSomeService(eq(node2), eq(data))).thenReturn(ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(""));
      when(connector.runSomeService(eq(node3), eq(data))).thenThrow(new ResourceAccessException(""));
  
      // when
      engine.runServiceOnAllNodes(data, collectors);
  
      // then
      verify(connector, times(1)).runSomeService(eq(node1), eq(data));
      verify(connector, times(1)).runSomeService(eq(node2), eq(data));
      verify(connector, times(1)).runSomeService(eq(node3), eq(data));
      verifyNoMoreInteractions(connector);
      assertEquals(node1.getStatus(), "OK");
      assertEquals(node2.getStatus(), "Fail");
  }
  </code></pre>
  
  <p>It can end with a few different results eg.</p>
  
  <pre><code>Wanted but not invoked:
  connector.runSomeService(node2);
  
  However, there were other interactions with this mock:
  connector.runSomeService(node1);
  </code></pre>
  
  <p>or</p>
  
  <pre><code>Argument(s) are different! Wanted:
  connector.runSomeService(node1);
  
  Actual invocation has different arguments:
  connector.deployFileset(node2);
  </code></pre>
  
  <p>or sometimes it ends with success.</p>
  
  <p>It is clear that the time of execution <code>connector.runSomeService()</code> and the time of the verification can interlace. The order of this two actions is not deterministic.</p>
  
  <p>Using sleep sucks. Tried to gather all responses and calling future.get() </p>
  
  <pre><code>// when
  engine.runServiceOnAllNodes(data, collectors);
  for (CompletableFuture future : engine.getResponses()) {
      future.get();
  }
  </code></pre>
  
  <p>but I'm getting some exception but I still have the feeling that this way also sucks, isn't it?</p>