Search⌘ K
AI Features

Assertions with AssertJ

Explore how to implement fluent assertions using the AssertJ library in Spring Framework tests. This lesson covers making readable and descriptive assertions for various data types including String, List, Map, Exception, and LocalDateTime to improve code clarity and error diagnostics.

AssertJ

We’ve used the Assertions class of the JUnit5 framework in all our unit and integration tests for comparing expected and actual results. However, the AssertJ library offers fluent assertion APIs with better code readability and error messages.

Setup

Let’s add the core AssertJ dependency in the build.gradle file of our todo application.

Diff
dependencies {
testImplementation "org.assertj:assertj-core:3.22.0"
}

The AssertJAssertions class

We’ll create the AssertJAssertions class to make a few assertions by using the AssertJ’s Assertions class.

Java
package io.educative.unit;
import static org.assertj.core.api.Assertions.*;
public class AssertJAssertions {
}

The String

...