Have you ever written an equals method, along with five screens of unit tests to test it? Or worse: not bothered to test it at all, because “the IDE generates it anyway”?

EqualsVerifier helps you. Testing equals can be as simple as:

import nl.jqno.equalsverifier.*;

@Test
public void equalsContract() {
    EqualsVerifier.forClass(Foo.class).verify();
}

EqualsVerifier is an opinionated library, which means that it can be quite strict. If you feel it’s too much, you can make it more lenient:

import nl.jqno.equalsverifier.*;

@Test
public void simpleEqualsContract() {
    EqualsVerifier.simple().forClass(Foo.class).verify();
}

And EqualsVerifier even gives you 100% coverage on equals and hashCode methods.

Don’t forget to add it to your build!

<dependency>
    <groupId>nl.jqno.equalsverifier</groupId>
    <artifactId>equalsverifier</artifactId>
    <version>3.17.1</version>
    <scope>test</scope>
</dependency>

or, for a ‘fat’ jar with no transitive dependencies:

<dependency>
    <groupId>nl.jqno.equalsverifier</groupId>
    <artifactId>equalsverifier-nodep</artifactId>
    <version>3.17.1</version>
    <scope>test</scope>
</dependency>

Prefer to watch a short video?

Video by Tom Cools

A note on equality

EqualsVerifier cares about bug-free equality, in Java and in real life. The place where a person happens to be born, the colour of their skin, their gender, or the person they happen to love, must not affect the way they are treated in life. If it does, that’s a bug and it should throw an error.

Don’t allow bugs in your equality.

🌈🧑🏻‍🤝‍🧑🏾🌍