Skip to content

· 1 min read

Writing cleaner tests with Fluent Assertions

Fluent Assertions make tests highly readable and English-like, presenting a concise structure that simplifies comprehension

Your tests should be very easy to read essentially english like. There are multiple techniques to achieve this.

One of the ways is having fluent assertions. Fluent assertions are assertions chained together. The structure is such that it makes it more concise and easy to read.

We’ll take an example comparing Junit assertions with AssertJ assertions.

In the below example, we are checking if a Map contains specific keys, and if date is correct.

Map<String, String> jsonMessage = {someServiceCallReturningAJson};

// JUnit Assertions: Not Fluent
Set<String> expectedKeys = new HashSet<>(Arrays.asList("date", "userId", "randomField"));
Assertions.assertEquals(expectedKeys, jsonMessage.keySet());
Assertions.assertEquals("2001-07-04", jsonMessage.get("date"));

// AssertJ: Fluent Assertions
assertThat(jsonMessage)
    .containsKeys("date", "userId", "randomField")
    .containsEntry("date", "2001-07-04");

The JUnit assertion is bulkier and will continue to get much bulkier with more assertion.

The AssertJ fluent assertion on other hand is grouped, contains helper methods that do not require specially conversion of object for common comparision.

Get new posts by email

No spam. Unsubscribe anytime. Or grab the RSS feed.

Mailto-based for now. ButtonDown / ConvertKit can replace this form later.


Comments lazy-load on scroll. Prefer GitHub? Giscus activates once PUBLIC_GISCUS_* env vars are set (see Giscus.astro).

Index

Index

Back to Blog