A quick introduction to AssertJ

As a developer, you might be familiar with Hamcrest, a declarative rule based object matcher framework. In other words, a testing library which will make your test easy to read. While Hamcrest is a great library, there are some limitations to it’s expressiveness, so looked for a better alternative. Meet AssertJ.

AssertJ is a library which provides a rich set of asserts and a fluent interface to help developers write maintainable tests. Besides that, most of the AssertJ error messages in the case of failing tests are very expressive and helpful, and provide great feedback to the developers in finding out why a test failure occurred.

To setup AssertJ in your project, the only library you’ll need is the assertj-core library. I’ve set up a sample project on Github to help you get started.

To make sure you’re importing the right assertThat method, we’ll be excluding the JUnit method:

exclude-auto-import

To make our lives even easier, we’ll setup an IntelliJ Live Template, which will map the ‘at’ abbreviation to expand to assertThat’s assertion method. This way, we only need to type ‘at’, press , and the right assertThat method will be used, including imports.

live-template

In this example we’ll be testing a small movie catalogue, in which most logic is located in finding the movies, and, in our case, only thrillers:

The code above uses the Guava FluentIterator to filter movies based on certain criteria. In this case a filter will be applied to find only Thrillers. To test this, we write the following AssertJ test:

What happens here, is that for each element in the list of Thrillers, the ‘title’ property is extracted, and an exact match is done on the result of the thrillers result, ie. it should have exactly those 4 elements.

One of the great features of AssertJ is that assertThat is a generic method. This means that your IDE will help you a great deal to know which methods are available to help in testing, since it depends on the type of the object passed to assertThat. To illustrate this, see the following example:

code-completion-list

As you can see here, only the methods which can be applied to a List are shown. When we replace the List with a String

code-completion-error

IntelliJ immediately responds with a compilation error, and when using code completion again, you’ll get the list of methods which are compatible with a String type:

code-completion-string

Besides providing a hints to developers and giving early feedback, AssertJ also enables you to have very readable tests. An example can be seen below:

Here, we extract multiple attributes of a Movie, and we’ll compare them with our actual result. Should a comparison fail, an error is shown:

In this, we expected the movie ‘Se8en’ to be there, but we made an obvious typo, which could be corrected quickly.

I hope this short introduction gave you a impression of what is possible with AssertJ. More examples can be found in the same project or on the AssertJ website. AssertJ provides many more features, like powerful test DSL’s, auto generated asserts for your own types, support for Joda, Guava and Neo4j, and much more.

To find out more, checkout AssertJ and start improving your test code!