EqualsVerifier can be used in Java unit tests to verify whether the contract for the equals and hashCode methods in a class is met.
EqualsVerifier's Maven coordinates are:
<dependency>
<groupId>nl.jqno.equalsverifier</groupId>
<artifactId>equalsverifier</artifactId>
<version>4.0.7</version>
<scope>test</scope>
</dependency>
(Note that there's also a 'fat' jar with no transitive dependencies with artifactId equalsverifier-nodep
.)
Now you can write a test:
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 equalsContract() {
EqualsVerifier.simple().forClass(Foo.class).verify();
}
This way, EqualsVerifier will throw less errors at you. However, it's usually better to just fix the errors: EqualsVerifier throws them for a reason!
Video by Tom Cools
For more documentation, see:
- The changelog
- The project's website
- The ARCHITECTURE.md file
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.
🌈🧑🏻🤝🧑🏾🌍
Pull requests are welcome! If you plan to open one, please also register an issue or start a discussion, so we can discuss it first. It would be a shame to put in a lot of work on something that isn't a good fit for the project. Also, I can help you by giving pointers on where to find certain things.
Copyright 2009-2025 Jan Ouwens