AssertJ for Java: A Complete Guide with Examples

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

AssertJ for Java: A Complete Guide to Fluent Assertions

\n\n

1. What is it and why is it needed

\n\n

AssertJ is a powerful fluent-assertions library for the Java language, designed for writing readable, expressive, and self-documenting tests. Unlike standard JUnit or TestNG, which provide only basic methods like assertEquals(a, b), AssertJ offers a rich API with method chaining, making test code resemble natural language. For example, instead of assertTrue(list.contains("foo")), you write assertThat(list).contains("foo").

\n\n

Why is it needed? First, AssertJ significantly improves test readability. When a developer sees assertThat(result).isNotNull().hasSize(5).contains("expected"), they immediately understand what is being checked: the result is not null, its size is 5, and it contains the string "expected". Second, AssertJ provides detailed error messages. Instead of a boring "expected true but was false", you will get "Expected list to contain 'foo' but could not find it in [bar, baz]". Third, the library supports many data types: collections, strings, numbers, dates, files, exceptions, and even custom objects.

\n\n

AssertJ is especially useful in projects where test base maintainability is important. Thanks to the fluent API, tests become less fragile during refactoring, as method chains are easy to modify. The library is actively developed, has excellent documentation, and is widely used in the industry (Spring, Hibernate, Netflix, etc.).

\n\n

2. Installation

\n\n

AssertJ is distributed via Maven Central. To use it in a project, add the dependency to your pom.xml (Maven) or build.gradle (Gradle).

\n\n

Maven

\n
<dependency>\n    <groupId>org.assertj</groupId>\n    <artifactId>assertj-core</artifactId>\n    <version>3.25.3</version> <!-- Check the latest version on Maven Central -->\n    <scope>test</scope>\n</dependency>\n
\n\n

Gradle

\n
testImplementation 'org.assertj:assertj-core:3.25.3'\n
\n\n

If you are using JUnit 5, no additional dependencies are required. AssertJ also works without issues with JUnit 4. Make sure the AssertJ version is compatible with your Java version (3.x requires Java 8 or higher).

\n\n

3. Quick Start — Minimal Working Example

\n\n

After adding the dependency, you can start using AssertJ immediately. The main method is assertThat(), which takes an object and returns the corresponding assertion object.

\n\n
import static org.assertj.core.api.Assertions.assertThat;\n\nimport org.junit.jupiter.api.Test;\n\npublic class QuickStartTest {\n\n    @Test\n    void testSimpleAssertion() {\n        String name = "AssertJ";\n        \n        // Basic assertion\n        assertThat(name)\n            .isNotNull()\n            .isNotEmpty()\n            .startsWith("Ass")\n            .endsWith("J")\n            .hasSize(7);\n        \n        // Number check\n        int number = 42;\n        assertThat(number)\n            .isPositive()\n            .isGreaterThan(40)\n            .isLessThan(50);\n        \n        // Collection check\n        var list = java.util.List.of("a", "b", "c");\n        assertThat(list)\n            .hasSize(3)\n            .contains("a", "b")\n            .doesNotContain("z");\n    }\n}\n
\n\n

This example demonstrates the key idea: a chain of methods, each returning the same assertion object, allowing you to write sequential checks. If any assertion fails, the test fails with a clear message.

\n\n

4. Main Methods/Functions/Classes

\n\n

AssertJ provides hundreds of methods. Below are 8 of the most frequently used with signatures

Recommendations