Mockito and asynchronous code

On Friday, I talked about using Awaitility. It’s a good tool to black-box test asynchronous code. You trigger an action and then wait for a predicate to be true.

Another tool I use a lot to test asynchronous code is Mockito. People tend to forget that Mockito is able to verify calls on a mock with the same kind of polling mechanism as Awaitility.

Here’s a code sample:

  @Test
  public void async() {
    Receiver receiver = mock(Receiver.class);

    Sender sender = new Sender(receiver);
    sender.post("hello");

    verify(receiver, timeout(1000)).onMessage("hello");
  }

Simple, yet very effective.

Mockito is more for white-box testing than Awaitility.

Also, remember that the easiest way to test asynchronous code is to make it synchronous!