Unit testing is essential for verifying that individual units of code function as expected. Writing multiple assertions can make tests lengthy and difficult to maintain when testing object properties. Testing frameworks like JUnit and AssertJ provide mechanisms to combine various assertions into a single call, enhancing both readability and feedback when tests fail.
In Java unit testing, it is common to check various properties of an object. For example, when testing an Employee object, you may want to verify the employee's name, job title, and age. Writing separate assertions for each property can result in lengthy and cluttered tests. To address this, modern frameworks like JUnit 5 and AssertJ allow grouping assertions into a single block that makes our tests cleaner and easier to manage.
This article explains how to combine multiple assertions into a single call, providing examples using JUnit 5 and AssertJ.
Why Unit Testing is Important?
Unit testing involves checking individual units of code in isolation to confirm their correctness. Automated unit tests are run frequently to ensure that new code changes do not introduce bugs into existing functionality. The key component of unit testing is assertions, which verify that the actual output matches the expected result.
For example, in a test for the Employee class, we might assert that an employee’s name is "John," job title is "Developer," and age is 28. By using assertions, we ensure that the code behaves as intended.
Problem with Multiple Assertions
In traditional unit testing, each property of an object is typically tested with a separate assertion. For example:
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.Test;
class EmployeeTest {
@Test
void testEmployeeProperties() {
Employee employee = new Employee("John", "Developer", 28);
assertEquals("John", employee.getName());
assertEquals("Developer", employee.getJobTitle());
assertEquals(28, employee.getAge());
}
}
While this approach works, it has some drawbacks:
- Repetitiveness: Every property requires a separate line of code.
- Reduced readability: Multiple assertions can clutter tests.
- Poor feedback: If one assertion fails, the others are not executed, limiting feedback.
Solution: Single Assert Call for Multiple Properties
To make tests more concise and readable, you can use a single assert call to group related assertions.
Using JUnit's assertAll
JUnit 5 introduced the assertAll method, allowing developers to group multiple assertions in a single test block. If one assertion fails, JUnit still evaluates the others and reports all failures at once.
Example:
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.Test;
class EmployeeTest {
@Test
void testEmployeeProperties() {
Employee employee = new Employee("John", "Developer", 28);
assertAll("employee",
() -> assertEquals("John", employee.getName(), "Name should be John"),
() -> assertEquals("Developer", employee.getJobTitle(), "Job title should be Developer"),
() -> assertEquals(28, employee.getAge(), "Age should be 28")
);
}
}
In this example,
assertAllgroups assertions logically.- Each assertion inside
assertAllis a lambda expression. Even if one fails, the rest are executed, and all failures are reported together.
Using AssertJ for Fluent Assertions
AssertJ provides a fluent and expressive way to write assertions, allowing you to chain them together, making tests more readable and concise.
Example:
import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.Test;
class EmployeeTest {
@Test
void testEmployeeProperties() {
Employee employee = new Employee("John", "Developer", 28);
Assertions.assertThat(employee)
.extracting(Employee::getName, Employee::getJobTitle, Employee::getAge)
.containsExactly("John", "Developer", 28);
}
}
In this example:
extractingis used to fetch multiple properties from the object.containsExactlyasserts that the extracted properties match the expected values in order.
Project Implementation of Single Assert Call for Multiple Properties
Create an example project that demonstrates how to use the single assert call for the multiple properties in the Java unit testing. We will use the simple Employee class and write the unit tests with both JUnit 5 and AssertJ for the asserting multiple properties of the maven project.
Step 1: Create a Maven Project
In your IDE (e.g., IntelliJ IDEA), create a new Maven project with the following details:
- Name:
single-assert-example - Build System: Maven
Click on the Create button.

Project Structure
After project creation done, then the folder structure will look like the below image:

Step 2: Add Dependencies to pom.xml
Open the pom.xml file and add the following JUnit dependencies.
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="https://maven.apache.org/POM/4.0.0"
xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="https://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.gfg</groupId>
<artifactId>single-assert-example</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<!-- JUnit 5 Dependency -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.8.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.8.2</version>
<scope>test</scope>
</dependency>
<!-- AssertJ Dependency -->
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>3.21.0</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<!-- Maven Surefire Plugin for running tests -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.2</version>
</plugin>
</plugins>
</build>
</project>
Step 3: Create Employee Class
The Employee class models an employee object with three fields that are name, jobTitle, and age.
package com.gfg;
public class Employee {
private String name;
private String jobTitle;
private int age;
public Employee(String name, String jobTitle, int age) {
this.name = name;
this.jobTitle = jobTitle;
this.age = age;
}
public String getName() {
return name;
}
public String getJobTitle() {
return jobTitle;
}
public int getAge() {
return age;
}
@Override
public String toString() {
return "Employee{" +
"name='" + name + '\'' +
", jobTitle='" + jobTitle + '\'' +
", age=" + age +
'}';
}
}
Step 4: Main Class
package com.gfg;
public class Main {
public static void main(String[] args) {
Employee employee = new Employee("Alice", "Software Engineer", 30);
// Printing the employee details using the overridden toString() method
System.out.println(employee);
// Accessing the individual properties
System.out.println("Name: " + employee.getName());
System.out.println("Job Title: " + employee.getJobTitle());
System.out.println("Age: " + employee.getAge());
}
}
Step 5: JUnit 5 Test
Create the EmployeeTest class and this test file demonstrates how to use the JUnit 5 assertAll to test the multiple properties of the Employee object in the single assertion block.
EmployeeTest.java:
import static org.junit.jupiter.api.Assertions.*;
import com.gfg.Employee;
import org.junit.jupiter.api.Test;
class EmployeeTest {
@Test
void testEmployeeProperties() {
// Create an Employee object
Employee employee = new Employee("John", "Developer", 28);
// Use assertAll to group assertions
assertAll("employee",
() -> assertEquals("John", employee.getName(), "Name should be John"),
() -> assertEquals("Developer", employee.getJobTitle(), "Job title should be Developer"),
() -> assertEquals(28, employee.getAge(), "Age should be 28")
);
}
}
Step 6: AssertJ Test
Create the EmployeeAssertJTest class and this test demonstrates how to use the AssertJ for the fluent and expressive way of the asserting multiple properties.
EmployeeAssertJTest.java:
import com.gfg.Employee;
import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.Test;
class EmployeeAssertJTest {
@Test
void testEmployeeProperties() {
// Create an Employee object
Employee employee = new Employee("John", "Developer", 28);
// Using AssertJ to assert multiple properties in one go
Assertions.assertThat(employee)
.extracting(Employee::getName, Employee::getJobTitle, Employee::getAge)
.containsExactly("John", "Developer", 28);
}
}
Step 7: Run the Application
Now, run the application, and it will show the below output in console.

Step 8: Testing the Application
Now, we will run the test cases using the below command:
mvn testOutput:

Conclusion
Combining multiple assertions into a single call, either using JUnit’s assertAll or AssertJ’s fluent API, can significantly improve the readability and maintainability of your tests. These tools also allow better failure feedback by continuing to check all assertions, even if one fails.