title: In JUnit with Mockito, how can I wait for an asynchronous method to complete?
body: <p>I am writing an integration test against some code that asynchronously creates a record when a different value is updated in the database.  I would like to check the state of the system after the record is created, verifying that it was created as expected.  The test therefore needs to wait until the record is created.</p>
  
  <p>I can use Mockito to create a spy for the function that creates the record.  Mockito even has the option to wait for the method to be called via <code>Mockito.timeout</code>, giving up if a certain amount of time has elapsed without the method being called:</p>
  
  <pre><code>// Use or create/wire in spy. In my case, this is set up with @SpyBean from spring-boot-test.
  RecordCreationService recordCreationServiceSpy = ...;
  
  testClass.update(someValue);
  
  Mockito.verify(recordCreationServiceSpy, Mockito.timeout(10_000)).createRecord(ArgumentMatchers.any());
  </code></pre>
  
  <p>However, this merely waits for the call to have started, not for it to have been completed.  Thus, this enters a race condition where the verification can finish before the desired call completes.</p>
  
  <p>How can I cleanly and simply wait for the completion of a process before verifying in JUnit with Mockito?</p>