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.

Installation

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

<dependency>
 <groupId>org.assertj</groupId>
 <artifactId>assertj-core</artifactId>
 <version>3.19.0</version>
 <scope>test</scope>
</dependency>
Enter fullscreen mode Exit fullscreen mode

Usage

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 {

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

 Assertions.assertThat(x).isEqualTo(y);
 }

 @Data
 @AllArgsConstructor
 private static class TestedDto {

 String firstString;
 String secondString;
 TestedNestedDto nested;
 }

 @Data
 @AllArgsConstructor
 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.

org.opentest4j.AssertionFailedError: 
Expecting:
 <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:

 @Test
 void testComparison() {

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

 Assertions.assertThat(x.getFirstString())
 .startsWith("D")
 .endsWith("n")
 .isLowerCase();
 }
Enter fullscreen mode Exit fullscreen mode

This will output

java.lang.AssertionError: 
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.

 @Test
 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);

 Assertions.assertThat(collection)
 .hasSize(2)
 .contains(x)
 .allMatch(tested -> tested.getNested().getFirstInt() == 1)
 .anyMatch(tested -> tested.getSecondString().equals("b"))
 .containsNull();
 }
Enter fullscreen mode Exit fullscreen mode

Prints this error message

java.lang.AssertionError: 
Expecting:
 <[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)

Collapse
 
nismooooooo profile image
Anton Dreka

thanks for the article.