Testing equals() with Guava

I've long held the opinion that Guava belongs on every Java classpath because its design and the quality of its implementation surpass other comparable utility libraries (and sometimes even the JDK). Guava's testlib is pretty cool, too, even though it's not as widely known.

Most things in testlib are useful for testing self-built collection classes (which hopefully few people need), but there's a couple of things for the rest of us. One such thing is the EqualsTester. It's much more work to test an equals() method properly than to actually implement it. You have to test for all equivalence relation properties and additionally for null values. That's why many people don't bother.

With EqualsTester, you specify equivalence groups. Objects within a group must be equal to each other while objects between groups must not be equal. In other words, an object in one group must not be equal to any other object except for other members of its own equivalence group.

First of all, add testlib to your classpath using the following Maven coordinates:

<dependency>
  <groupId>com.google.guava</groupId>
  <artifactId>guava-testlib</artifactId>
  <version>17.0</version>
</dependency>

And here's how it works, using an example from testlib's Javadoc:

new EqualsTester()
    .addEqualityGroup("hello", "h" + "ello")
    .addEqualityGroup("world", "wor" + "ld")
    .addEqualityGroup(2, 1 + 1)
    .testEquals();

Testing hashCode() isn't quite as powerful. You can only test for consistency with equals(), which is exactly what EqualsTester does. A hashCode() test like this won't stop you from doing something stupid, like returning a constant, but after all, that's valid as per the hashCode() contract.

social