Partner – Moderne – NPI EA (cat=Spring Boot)
announcement - icon

Refactor Java code safely — and automatically — with OpenRewrite.

Refactoring big codebases by hand is slow, risky, and easy to put off. That’s where OpenRewrite comes in. The open-source framework for large-scale, automated code transformations helps teams modernize safely and consistently.

Each month, the creators and maintainers of OpenRewrite at Moderne run live, hands-on training sessions — one for newcomers and one for experienced users. You’ll see how recipes work, how to apply them across projects, and how to modernize code with confidence.

Join the next session, bring your questions, and learn how to automate the kind of work that usually eats your sprint time.

Course – Black Friday 2026 – NPI EA – main – before_post_content
announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 1st December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

Course – Black Friday 2026 – NPI – main – before_post_content
announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 1st December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

1. Overview

Sometimes, we don’t have the ability to override the equals() method in a class. Nevertheless, we’d still like to compare one object against another to check whether they are the same.

In this tutorial, we’ll learn a few ways for testing the equality of two objects without using the equals() method.

2. Example Classes

Before we dive in, let’s create the classes we’ll use through our examples. We’ll use the Person and Address classes:

public class Person {
    
    private Long id;
    private String firstName;
    private String lastName;
    private Address address;

    // getters and setters
}

public class Address {

    private Long id;
    private String city;
    private String street;
    private String country;

    // getters and setters
}

We didn’t override the equals() method in the classes. The default implementation given in the Object class will therefore be executed when determining equality. To put it differently, Java checks whether two references point to the same object when checking for equality.

3. Using AssertJ

AssertJ library provides a way to compare objects using recursive comparison. Using introspection, it determines which fields and values should be compared.

Firstly, to use the AssertJ library, let’s add the assertj-core dependency in the pom.xml:

<dependency>
    <groupId>org.assertj</groupId>
    <artifactId>assertj-core</artifactId>
    <version>3.26.0</version>
    <scope>test</scope>
</dependency>

To check whether fields in two Person instances contain the same values, we’ll use the usingRecursiveComparison() method before calling the isEqualTo() method:

Person expected = new Person(1L, "Jane", "Doe");
Person actual = new Person(1L, "Jane", "Doe");

assertThat(actual).usingRecursiveComparison().isEqualTo(expected);

Additionally, the algorithm gets the fields of an actual object and then compares them to the corresponding fields of an expected object. However, the comparison doesn’t work in a symmetrical way. The expected object can have more fields than the actual one.

Furthermore, we can ignore a certain field using the ignoringFields() method:

Person expected = new Person(1L, "Jane", "Doe");
Person actual = new Person(2L, "Jane", "Doe");

assertThat(actual)
  .usingRecursiveComparison()
  .ignoringFields("id")
  .isEqualTo(expected);

Additionally, it works efficiently when we want to compare complex objects:

Person expected = new Person(1L, "Jane", "Doe");
Address address1 = new Address(1L, "New York", "Sesame Street", "United States");
expected.setAddress(address1);

Person actual = new Person(1L, "Jane", "Doe");
Address address2 = new Address(1L, "New York", "Sesame Street", "United States");
actual.setAddress(address2);

assertThat(actual)
  .usingRecursiveComparison()
  .isEqualTo(expected);

4. Using Hamcrest

The Hamcrest library uses reflection to check whether two objects contain the same properties. Additionally, it creates a matcher to check whether the actual object contains the same values as the expected object.

Firstly, let’s add the Hamcrest dependency in the pom.xml:

<dependency>
    <groupId>org.hamcrest</groupId>
    <artifactId>hamcrest</artifactId>
    <version>2.2</version>
    <scope>test</scope>
</dependency>

Now, let’s call the sameProperyValuesAs() method and pass the expected object:

Person expected = new Person(1L, "Jane", "Doe");
Person actual = new Person(1L, "Jane", "Doe");

MatcherAssert.assertThat(actual, samePropertyValuesAs(expected));

Similar to the previous example, we can pass the names of the fields we’d like to ignore. They will be dropped from both the expected and actual object.

However, behind the scenes, Hamcrest uses reflection to get values from certain fields. When checked for equality, the method equals() will be called on each field.

That is to say, the code above won’t work if we’re using complex objects since we didn’t override the equals() method in the Address class either. Thus, it’ll check whether two Address references refer to the same object in memory. Therefore, the assertion will fail.

If we’d like to compare complex objects, we’ll need to compare them separately:

Person expected = new Person(1L, "Jane", "Doe");
Address address1 = new Address(1L, "New York", "Sesame Street", "United States");
expected.setAddress(address1);

Person actual = new Person(1L, "Jane", "Doe");
Address address2 = new Address(1L, "New York", "Sesame Street", "United States");
actual.setAddress(address2);

MatcherAssert.assertThat(actual, samePropertyValuesAs(expected, "address"));
MatcherAssert.assertThat(actual.getAddress(), samePropertyValuesAs(expected.getAddress()));

Here, we first excluded the address field from the first assertion and compared it in the second.

5. Using Apache Commons Lang3

Now, let’s see how we can check for equality using the Apache Commons library.

We’ll add the Apache Commons Lang3 dependency in our pom.xml:

<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-lang3</artifactId>
    <version>3.14.0</version>
    <scope>test</scope>
</dependency>

5.1. ReflectionToStringBuilder Class

One of the classes Apache Commons provides is the ReflectionToStringBuilder class. It allows us to generate a string representation of an object by reflecting on its fields and values.

By comparing the string representations of two objects, we can assert their equality without requiring the use of the equals() method:

Person expected = new Person(1L, "Jane", "Doe");
Person actual = new Person(1L, "Jane", "Doe");

assertThat(ReflectionToStringBuilder.toString(actual, ToStringStyle.SHORT_PREFIX_STYLE))
  .isEqualTo(ReflectionToStringBuilder.toString(expected, ToStringStyle.SHORT_PREFIX_STYLE));

However, we’d still need to override the toString() method in our classes.

5.2. EqualsBuilder Class

Alternatively, we can use the EqualsBuilder class:

Person expected = new Person(1L, "Jane", "Doe");
Person actual = new Person(1L, "Jane", "Doe");

assertTrue(EqualsBuilder.reflectionEquals(expected, actual));

It uses Java reflection API to compare the fields of two objects. It’s important to note that the reflectionEquals() method uses a shallow equality check.

Therefore, when comparing two complex objects, we’d need to ignore those fields and compare them separately

Person expected = new Person(1L, "Jane", "Doe");
Address address1 = new Address(1L, "New York", "Sesame Street", "United States");
expected.setAddress(address1);

Person actual = new Person(1L, "Jane", "Doe");
Address address2 = new Address(1L, "New York", "Sesame Street", "United States");
actual.setAddress(address2);

assertTrue(EqualsBuilder.reflectionEquals(expected, actual, "address"));
assertTrue(EqualsBuilder.reflectionEquals(expected.getAddress(), actual.getAddress()));

6. Using Mockito

Another way we can assert the equality of two instances is by using the Mockito.

We’ll need the mockito-core dependency:

<dependency>
    <groupId>org.mockito</groupId>
    <artifactId>mockito-core</artifactId>
    <version>5.11.0</version>
    <scope>test</scope>
</dependency>

Now, we can use Mockito’s ReflectionEquals class:

Person expected = new Person(1L, "Jane", "Doe");
Person actual = new Person(1L, "Jane", "Doe");

assertTrue(new ReflectionEquals(expected).matches(actual));

Furthermore, the EqualsBuilder from the Apache Commons library will be called when checking for equality.

Once again, we’ll need to use the same patch as we did with EqualsBuilder for the comparison of the complex objects:

Person expected = new Person(1L, "Jane", "Doe");
Address address1 = new Address(1L, "New York", "Sesame Street", "United States");
expected.setAddress(address1);

Person actual = new Person(1L, "Jane", "Doe");
Address address2 = new Address(1L, "New York", "Sesame Street", "United States");
actual.setAddress(address2);

assertTrue(new ReflectionEquals(expected, "address").matches(actual));
assertTrue(new ReflectionEquals(expected.getAddress()).matches(actual.getAddress()));

7. Conclusion

In this article, we learned how to assert the equality of two instances without using the equals() method.

To sum up, AssertJ’s field-by-field comparison offers the simplest way to compare complex objects, while other approaches use a reflection to compare fields, so we’d need to add an additional assertion for complex fields.

By leveraging the tools mentioned in this article, we can write tests even when facing objects without the equals() method.

The code backing this article is available on GitHub. Once you're logged in as a Baeldung Pro Member, start learning and coding on the project.
Partner – Moderne – NPI EA (tag=Refactoring)
announcement - icon

Modern Java teams move fast — but codebases don’t always keep up. Frameworks change, dependencies drift, and tech debt builds until it starts to drag on delivery. OpenRewrite was built to fix that: an open-source refactoring engine that automates repetitive code changes while keeping developer intent intact.

The monthly training series, led by the creators and maintainers of OpenRewrite at Moderne, walks through real-world migrations and modernization patterns. Whether you’re new to recipes or ready to write your own, you’ll learn practical ways to refactor safely and at scale.

If you’ve ever wished refactoring felt as natural — and as fast — as writing code, this is a good place to start.

Course – Black Friday 2026 – NPI EA – main – after_post_content

announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 1st December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

Course – Black Friday 2026 – NPI – main – after_post_content

announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 1st December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW