Supercharge Your Java Testing: Discover the Power of AssertJ

Haridwar Rajshri
2 min readJul 10, 2024

--

The AssertJ library is a powerful assertion library for Java that provides a fluent API for writing assertions. Its expressive syntax and extensive features make it a popular choice for creating readable and maintainable tests. Here’s a brief overview of AssertJ with code examples to illustrate its key features.

1. Fluent Assertions

AssertJ provides a fluent API, making assertions more readable and expressive.

import static org.assertj.core.api.Assertions.assertThat;

String name = "AssertJ Library";
assertThat(name).isNotNull()
.startsWith("Assert")
.endsWith("Library")
.contains("J");

2. Collection Assertions

AssertJ offers comprehensive support for collections, allowing you to perform a wide range of assertions on collections.

import static org.assertj.core.api.Assertions.assertThat;

List<String> names = Arrays.asList("Alice", "Bob", "Charlie");
assertThat(names).hasSize(3)
.contains("Alice", "Bob")
.doesNotContain("David");

3. Exception Assertions

With AssertJ, you can easily verify that your code throws the expected exceptions and check their properties.

import static org.assertj.core.api.Assertions.assertThatThrownBy;

assertThatThrownBy(() -> { throw new IllegalArgumentException("Invalid argument"); })
.isInstanceOf(IllegalArgumentException.class)
.hasMessage("Invalid argument");

4. Custom Assertions

AssertJ allows you to create custom assertions to cater to your specific needs, making it highly extensible.

import static org.assertj.core.api.Assertions.assertThat;

class Person {
String name;
int age;

Person(String name, int age) {
this.name = name;
this.age = age;
}
}

Person person = new Person("John Doe", 25);
assertThat(person).extracting("name", "age")
.containsExactly("John Doe", 25);

5. Soft Assertions

Soft assertions allow the test to continue after an assertion failure, collecting all errors and reporting them at the end.

import static org.assertj.core.api.SoftAssertions.assertSoftly;

assertSoftly(softly -> {
softly.assertThat("Hello").isEqualTo("Hello");
softly.assertThat(42).isGreaterThan(40);
softly.assertThat("AssertJ").startsWith("As").endsWith("J");
});

6. BDD Style Assertions

AssertJ supports Behavior-Driven Development (BDD) style assertions, making it more natural for those familiar with BDD.

import static org.assertj.core.api.BDDAssertions.then;

String message = "Hello BDD";
then(message).isNotEmpty()
.contains("BDD")
.doesNotContain("JUnit");

Conclusion

The AssertJ library enhances the process of writing assertions in Java by providing a fluent, expressive API and extensive features. Its support for collections, exceptions, custom assertions, soft assertions, and BDD style make it a powerful tool for creating clean, readable, and maintainable tests. Integrating AssertJ into your testing framework can significantly improve the quality and clarity of your test code.

--

--

Haridwar Rajshri
0 Followers

Software Test Engineer, Enthusiasts, Always Learning