I came across 2 interesting APIs to make testing easier this week…
The first is Awaitility which makes it easy to test asynchronous systems, it allows you to express a condition to wait for using a fluent API that encapsulates all the threading and retry logic that would otherwise be required. It works by polling for the condition to be met for a defined interval, failing if the condition is not met within that time.
The example below demonstrates one way the code could wait for single async insert to occur:
Getting started is easy, just add the maven dependency:
There are many configuration options (e.g. poll delay/frequency) and different way to express the condition to be met so it is well worth reading the simple and concise documentation here.
Beware, I had trouble with the latest version and wasn’t getting my conditions retried until I downgraded to version 1.6.1.
The second is JsonUnit which allows you to compare JSON in a far more flexible manner. For example you can ignore certain fields (for example if they will contain a generated value) or ignore array ordering when you only care each element is present. A simple example is below:
You can also easily compare the JSON against some regular expression. For example you might want to see if a UUID was present using the string below:
Getting started is easy, just add the maven dependency and check out the documentation: