Using AssertJ

AssertJ (https://github.com/joel-costigliola/assertj-core) 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

asserNotNull(list);
assertEquals(3, list.size());

can be replaced by

assertThat(list).isNotNull().hasSize(3);

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
assertThat(birthday).isToday();
// check if aDate is Aug 8th, 2013
assertThat(aDate).isEqualTo(“2013-08-08”);

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 (http://joel-costigliola.github.io/assertj/core/api/index.html) but also a good source of examples in https://github.com/joel-costigliola/assertj-examples/ where you can get syntax and examples for your assertions.

Maven users can add AssertJ as test dependency

<dependency>
    <groupId>org.assertj</groupId>
    <artifactId>assertj-core</artifactId>
    <version>1.3.0</version> 
    <scope>test</scope>
</dependency>
Advertisements

One thought on “Using AssertJ

  1. Pingback: Tutorial: using Mockito | Gualtiero Testa

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s