I came across 2 interesting APIs to make testing easier this week…

Awaitility

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:

@Test
public void endToEndTest() {
    saveActivityAsync();
    await().until(activityCount(), equalTo(1));
}

private Callable<Integer> activityCount() {
    return () -> repository.getActivityCount();
}

Getting started is easy, just add the maven dependency:

<dependency>
      <groupId>com.jayway.awaitility</groupId>
      <artifactId>awaitility</artifactId>
      <version>1.6.3</version>
      <scope>test</scope>
</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.

JsonUnit

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:

String expectedResponse = "{\"test\":1}";
String actualResponse = "{\n\"test\": 1\n}"
assertThatJson(actualResponse).when(IGNORING_ARRAY_ORDER).isEqualTo(expectedResponse);

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:

String jsonUnitUuidRegex = "${json-unit.regex}[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}";

Getting started is easy, just add the maven dependency and check out the documentation:

<dependency>
    <groupId>net.javacrumbs.json-unit</groupId>
    <artifactId>json-unit-fluent</artifactId>
    <version>1.5.5</version>
    <scope>test</scope>
</dependency>