Thread.sleep()

To continue with writing integration tests. When testing asynchronous operations, such as sending message to a queue, waiting for a response and completing some task, we need to introduce some kind of wait time in the test. Instead of using Thread.sleep(), we can use something called Awaitility…

Documentation is pretty self explanatory…

Testing asynchronous systems is hard. Not only does it require handling threads, timeouts and concurrency issues, but the intent of the test code can be obscured by all these details. Awaitility is a DSL that allows you to express expectations of an asynchronous system in a concise and easy to read manner. For example:

@Test
public void updatesCustomerStatus() {
    // Publish an asynchronous message to a broker (e.g. RabbitMQ):
    messageBroker.publishMessage(updateCustomerStatusMessage);

    // Awaitility lets you wait until the asynchronous operation completes:
    await().atMost(5, SECONDS).until(customerStatusIsUpdated());
    ...
}

Awaitility.org

Getting started