Awaitility

Usually Mockito provides methods with timeout,
verify(…, timeout(…). But often it is not useful.
Thread.sleep() is a bad practise and may not work in some situations. Also it waits concrete amount of time (e.g 3000ms) and will always wait this interval, even if the operation took 300ms.

A nice solution to the problem is using the test dependency Awaitility.
It is extremely easy to use, has a fluent API, polls regularly and is configurable, so every test can be optimised.
The idea behind it, is that it polls every 100ms (by default, but configurable) and after 10sec (also by default and configurable) it announce the test failed if the condition is not met.

await()
.atLeast(1, SECONDS)
.atMost(5, SECONDS)
.until(imageInCdnIsDeleted(imageUrl));

So, instead of waiting every time 10 minutes for test to finish, because of the sleep time, the suite will finish for the (near) max time required for the operations. For example it may finish for 3.5minutes, but also for 8minutes.

Subscribe to Awaitility