DEV Community

Pavel Polívka
Pavel Polívka

Posted on

Improve your unit tests with AssertJ

You are probably writing unit tests, if not you probably should. Over the years of my career, I did a lot of workshops, speaks, etc about how to write those correctly. One of the points I always make is to have understandable outputs of those tests. Nothing is worst than failing a unit test with a message like

Failed. True != False

what are you supposed to do with that? With standard assertions functions provided by all testing frameworks, you can improve this a bit by using correct functions, providing additional messages, etc... It's not perfect and it sometimes is a lot of work.

Here comes AssertJ. It's a simple library designed to improve your assertions. I would consider it essential for my testing needs. It provides a vast variety of assertions, state of the art error messages. Also, it improves code readability, it's super simple to understand what you want to assert.


AssertJ is available on Maven central so installation is as simple as adding a test dependency.

Enter fullscreen mode Exit fullscreen mode


In this article, I will go over few examples of how awesome AssertJ is. These examples will be done in JUnit5 with the following structure.

class DtoComparisonTest {

 void testComparison() {
 var x = new TestedDto("a", "c", new TestedNestedDto(1, 2));
 var y = new TestedDto("a", "b", new TestedNestedDto(1, 3));


 private static class TestedDto {

 String firstString;
 String secondString;
 TestedNestedDto nested;

 private static class TestedNestedDto {
 int firstInt;
 int secondInt;

Enter fullscreen mode Exit fullscreen mode

This example compares the x and y objects and prints the following error messages.

 <DtoComparisonTest.TestedDto(firstString=a, secondString=c, nested=DtoComparisonTest.TestedNestedDto(firstInt=1, secondInt=2))>
to be equal to:
 <DtoComparisonTest.TestedDto(firstString=a, secondString=b, nested=DtoComparisonTest.TestedNestedDto(firstInt=1, secondInt=3))>
but was not.
Expected :DtoComparisonTest.TestedDto(firstString=a, secondString=b, nested=DtoComparisonTest.TestedNestedDto(firstInt=1, secondInt=3))
Actual :DtoComparisonTest.TestedDto(firstString=a, secondString=c, nested=DtoComparisonTest.TestedNestedDto(firstInt=1, secondInt=2))
Enter fullscreen mode Exit fullscreen mode

How awesome is that?

It has a lot of built-in assertions for String. Let's see some examples:

 void testComparison() {

 var x = new TestedDto("Dragon", "Goblin", new TestedNestedDto(1, 2));

Enter fullscreen mode Exit fullscreen mode

This will output

Expecting <"Dragon"> to be a lowercase
Enter fullscreen mode Exit fullscreen mode

Imagine doing this and having the same output with normal assertions.

It also has tons of built-ins for collections.

 void testComparison() {

 var x = new TestedDto("a", "b", new TestedNestedDto(1, 2));
 var y = new TestedDto("c", "d", new TestedNestedDto(1, 2));
 var collection = Arrays.asList(x, y);

 .allMatch(tested -> tested.getNested().getFirstInt() == 1)
 .anyMatch(tested -> tested.getSecondString().equals("b"))
Enter fullscreen mode Exit fullscreen mode

Prints this error message

 <[DtoComparisonTest.TestedDto(firstString=a, secondString=b, nested=DtoComparisonTest.TestedNestedDto(firstInt=1, secondInt=2)),
 DtoComparisonTest.TestedDto(firstString=c, secondString=d, nested=DtoComparisonTest.TestedNestedDto(firstInt=1, secondInt=2))]>
to contain a <null> element
Enter fullscreen mode Exit fullscreen mode

I am pretty much in love with this library. It's super simple to start with and improves your tests so much. I recommend you to start using it today (also dive into their documentation, there is so much more to AssertJ).


For more tips like this, you can follow me on Twitter.

Top comments (1)

nismooooooo profile image
Anton Dreka

thanks for the article.