Using AssertJ

AssertJ ( is an assertion library which can be used to improve our test units code.

I’ve found two significant benefits by using this library compared to other solutions I used (jUnit basic assertions, Hamcrest and Fest Assert 1.x).

First, its fluent interface (same as Fest Assert) let me express in a more compact and readable way the assertions.

For example, assuming list is a java.util.List, the following jUnit asserts

assertEquals(3, list.size());

can be replaced by


which I believe it is a more readable and compact code.

Second benefit is the wide range of checks we can do without need for conversion methods or additional libraries. For example, assuming birthday and aDate are java.util.Date instances:

// check if birthday is today
// check if aDate is Aug 8th, 2013

or, assuming aMap is a java.util.Map,

// check if aMap has all 3 keys
assertThat(aMap).containsKeys(key1, key2, key3);

Again we can have more readable and compact code but also we can be more specific and correct in our assertions.

In the AssertJ web site (hosted on GitHub) you can find the usual API documentation ( but also a good source of examples in where you can get syntax and examples for your assertions.

Maven users can add AssertJ as test dependency