- EqualsVerifier 3.x → Java 8 or higher
- EqualsVerifier 2.x → Java 7 or higher
- EqualsVerifier 1.x → Java 6 or higher
Check out this recording from my talk at Devoxx 2017:
First, add the EqualsVerifier dependency to your build script:
(please adjust for your build system of choice)
If you need to download the jar file directly, you can download it from maven.org. EqualsVerifier doesn’t have any transitive dependencies, so this jar is all you need.
Use it in a test
This is what EqualsVerfier can look like in your test:
Chances are, EqualsVerifier will give you an error message on the first try. That might be frustrating at first, but there’s a good reason for it. There’s a surprising number of ways in which an
equals method can contain bugs. Even when you let your IDE generate it, it might have problems.
These problems might not seem like a big deal, and to be honest, you might never encounter them anyway. But if you do, they can be incredibly hard to debug.
That’s why EqualsVerifier’s philosophy is to be super-strict by default. In fact, it can be frustratingly strict. But there’s a lot of ways to tweak EqualsVerifier to make it behave just the way you want it to.
Here are the most common issues you might run into on your first try:
If you still think EqualsVerifier is too strict, you can make it more lenient:
Warning.NONFINAL_FIELDS are automatically suppressed.
Want to learn more? Check out the manual!