AssertJ vs JUnit
Use AssertJ for fluent and more readable assertions.
What’s the difference between JUnit and AssertJ
Hi, there!
First of all, JUnit is an automated test framework that is built in Java, whereas AssertJ is an opensource library used for writing fluent and rich assertions in Java tests.
It’s true that you can write assertions just using JUnit, but if you ever have an extra half an hour for studying AssertJ, be sure to go for it. It will make your working process more effective and your code more readable.
Why should I use AssertJ?
- easy to learn: Ready to dive in AssertJ? You can find all about AssertJ here.
Took me less than half an hour to get AssertJ.
- easy to use: you just need to add a dependency and static import in your test class to start using AssertJ.
- fluent: AssertJ helps you to diversify your assertions.
- more readable code
- auto-completion: AssertJ provides auto-completion in IDEs. So you don't need to remember all method names.
To start with AssertJ let’s add a dependency. Here is a Maven dependency:
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>3.16.1</version>
<scope>test</scope>
</dependency>
And Gradle dependency:
testImplementation 'org.assertj:assertj-core:3.13.2'
Find the latest versions here. After that add a static import to your test class.
import static org.assertj.core.api.Assertions.*;
AssertJ in Action
Now let’s have a look at some code examples comparing JUnit and AssertJ assertions. Starting with assertEquals assertion. Here is how we usually do this in JUnit.
An here is AssertJ way. The main difference is that we put an object we want to compare first and then our assertion.
The main benefit of AssertJ is that it is set up to provide auto-completion in IDEs. Once you have specified the object you want to compare (using assertThat):
the IDE will understand the type of the comparison (in this case Object) and will show you all available assertions.
Also, it is often useful to describe the assertion performed, in AssertJ we can set the error message using
.as(String description)
Testing Collections
Here I will show just one example of using AssertJ when working with Collections. Here we are testing the TolkienCharacter class.
I used stream API and lambdas for writing tests using only JUnit.
With AssertJ it’s much easier. See it for yourself!
For more info about testing Collections with AssertJ, go here.
Testing Exceptions
In JUnit the Expected Exception rule allows us to expect an exception in the test case. Also, JUnit 4 lets you expect an exception through the @Test annotation’s expected attribute.
@Test(expected = IOException.class)
Let’s see how it works in this code example:
And in AssertJ we can do the same with only two lines of code. Isn’t that great!
Conclusion
AssertJ is a library for writing better assertions. Using AssertJ you can write cleaner and more readable code. Plus it is super easy to learn and use. AssertJ works with any Java version over Java 6, it also supports assertions using the newer features in Java such as lambdas. Find more about methods in AssertJ here. Happy testing!