1. Introduction
Ignoring fields during comparison with AssertJ is a powerful feature that helps to simplify tests by focusing only on the relevant parts of the objects. It’s beneficial when dealing with objects that have fields with dynamic or irrelevant values.
In this article, we’ll look at various methods within the AssertJ fluent API. We’ll start by setting the correct dependency and providing a simple example of an employee class. We’ll then explore various use cases for ignoring required fields within the object comparisons using methods provided by AssertJ.
2. Maven Dependency and Example Setup
Let’s start with setting up the required dependency enabling us to use this feature. We’ll add the assertj-guava dependency in pom.xml:
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-guava</artifactId>
<version>3.4.0</version>
</dependency>
Next, we’ll construct a simple Employee class with the following fields:
public class Employee {
public Long id;
public String name;
public String department;
public String homeAddress;
public String workAddress;
public LocalDate dateOfBirth;
public Double grossSalary;
public Double netSalary;
...// constructor
...//getters and setters
}
In the next sections, we’ll focus on methods provided by the AssertJ fluent API to ignore desired fields in Employee class tests in various cases.
3. Using ignoringFields()
Firstly, let’s see how we can specify one or more fields to ignore when comparing actual and expected objects. Here, we can use the APIs ignoringFields() method. It lets us specify which fields should be ignored during the comparison. This is particularly useful when we want to exclude fields that may vary between instances but are irrelevant to the test.
Let’ ‘s use the example of the Employee class created previously. We’ll create two instances of the Employee class and then compare them using AssertJ while ignoring some fields:
@Test
public void givenEmployeesWithDifferentAddresses_whenComparingIgnoringSpecificFields_thenEmployeesAreEqual() {
// Given
Employee employee1 = new Employee();
employee1.id = 1L;
employee1.name = "John Doe";
employee1.department = "Engineering";
employee1.homeAddress = "123 Home St";
employee1.workAddress = "456 Work Ave";
employee1.dateOfBirth = LocalDate.of(1990, 1, 1);
employee1.grossSalary = 100000.0;
employee1.netSalary = 75000.0;
Employee employee2 = new Employee();
employee2.id = 2L;
employee2.name = "John Doe";
employee2.department = "Engineering";
employee2.homeAddress = "789 Home St";
employee2.workAddress = "101 Work Ave";
employee2.dateOfBirth = LocalDate.of(1990, 1, 1);
employee2.grossSalary = 110000.0;
employee2.netSalary = 80000.0;
// When & Then
Assertions.assertThat(employee1)
.usingRecursiveComparison()
.ignoringFields("id", "homeAddress", "workAddress", "grossSalary", "netSalary")
.isEqualTo(employee2);
}
Here, we want to ignore fields namely id, homeAddress, workAddress, grossSalary, and netSalary when comparing two Employee objects. This means that equality holds even if all these ignored fields are different for employee1 and employee2.
4. Using ignoringFieldsMatchingRegexes()
AssertJ also provides the ignoringFieldsMatchingRegexes() method, allowing us to ignore fields based on regular expressions. This is useful when we have multiple fields with similar names that we may want to exclude.
Let’s assume we have an Employee class where fields like homeAddress, workAddress, etc., should be ignored in the comparison. Similarly, we want to ignore all the fields ending with “Salary”:
@Test
public void givenEmployeesWithDifferentSalaries_whenComparingIgnoringFieldsMatchingRegex_thenEmployeesAreEqual() {
Employee employee1 = new Employee();
employee1.id = 1L;
employee1.name = "Jane Smith";
employee1.department = "Marketing";
employee1.homeAddress = "123 Home St";
employee1.workAddress = "456 Work Ave";
employee1.dateOfBirth = LocalDate.of(1990, 1, 1);
employee1.grossSalary = 95000.0;
employee1.netSalary = 70000.0;
Employee employee2 = new Employee();
employee2.id = 2L;
employee2.name = "Jane Smith";
employee2.department = "Marketing";
employee2.homeAddress = "789 Home St";
employee2.workAddress = "101 Work Ave";
employee2.dateOfBirth = LocalDate.of(1990, 1, 1);
employee2.grossSalary = 98000.0;
employee2.netSalary = 72000.0;
Assertions.assertThat(employee1)
.usingRecursiveComparison()
.ignoringFields("id")
.ignoringFieldsMatchingRegexes(".*Address", ".*Salary")
.isEqualTo(employee2);
}
Here, all fields ending with Address and Salary are ignored using ignoringFieldMatchingRegex() in addition to the id field.
5. Using ignoringExpectedNullFields()
Finally, we’ll look into the ignoreExpectedNullFields() method in the AssertJ API. This method ignores fields that are null in the expected object during comparison. This is particularly useful when only some fields are significant in the expected object, and others aren’t set or are irrelevant.
Let’s suppose we want to compare two Employee objects, but in the expected object, some fields are null and should be ignored:
@Test
public void givenEmployeesWithNullExpectedFields_whenComparingIgnoringExpectedNullFields_thenEmployeesAreEqual() {
Employee expectedEmployee = new Employee();
expectedEmployee.id = null;
expectedEmployee.name = "Alice Johnson";
expectedEmployee.department = null;
expectedEmployee.homeAddress = null;
expectedEmployee.workAddress = null;
expectedEmployee.dateOfBirth = LocalDate.of(1985, 5, 15);
expectedEmployee.grossSalary = null;
expectedEmployee.netSalary = null;
Employee actualEmployee = new Employee();
actualEmployee.id = 3L;
actualEmployee.name = "Alice Johnson";
actualEmployee.department = "HR";
actualEmployee.homeAddress = "789 Home St";
actualEmployee.workAddress = "123 Work Ave";
actualEmployee.dateOfBirth = LocalDate.of(1985, 5, 15);
actualEmployee.grossSalary = 90000.0;
actualEmployee.netSalary = 65000.0;
Assertions.assertThat(actualEmployee)
.usingRecursiveComparison()
.ignoringExpectedNullFields()
.isEqualTo(expectedEmployee);
}
Here, the comparison is made between the expectedEmployee and actualEmployee object based on all the non-null fields in the expectedEmployee object.
6. Conclusion
In this tutorial, we looked at various methods provided by AssertJ to ignore certain fields during object comparisons in testing. Using methods like ignoringFields(), ignoringFieldsMatchingRegexes(), and ignoringExpectedNullFields(), we can make our tests more flexible and easy to maintain.
As always, the full implementation of this article can be found over on GitHub.