Course – LS – All

Get started with Spring and Spring Boot, through the Learn Spring course:

>> CHECK OUT THE COURSE

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.21.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.

As always, the entire source code is available over on GitHub.

Course – LS – All

Get started with Spring and Spring Boot, through the Learn Spring course:

>> CHECK OUT THE COURSE
res – REST with Spring (eBook) (everywhere)
Comments are open for 30 days after publishing a post. For any issues past this date, use the Contact form on the site.