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);
}