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.

Advertisements
This entry was posted in java and tagged , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s