Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
   * Copyright (C) 2007 The Guava Authors
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
 import static;
 import static junit.framework.Assert.assertEquals;
 import static junit.framework.Assert.assertTrue;
 import java.util.List;

Tester for equals() and hashCode() methods of a class.

To use, create a new EqualsTester and add equality groups where each group contains objects that are supposed to be equal to each other, and objects of different groups are expected to be unequal. For example:

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

This tests:

  • comparing each object against itself returns true
  • comparing each object against null returns false
  • comparing each object against an instance of an incompatible class returns false
  • comparing each pair of objects within the same equality group returns true
  • comparing each pair of objects from different equality groups returns false
  • the hash codes of any two equal objects are equal

When a test fails, the error message labels the objects involved in the failed comparison as follows:

  • "[group i, item j]" refers to the jth item in the ith equality group, where both equality groups and the items within equality groups are numbered starting from 1. When either a constructor argument or an equal object is provided, that becomes group 1.

Jim McMaster
Jige Yu
 public final class EqualsTester {
   private static final int REPETITIONS = 3;
   private final List<List<Object>> equalityGroups = Lists.newArrayList();
   private final RelationshipTester.ItemReporter itemReporter;

Constructs an empty EqualsTester instance
   public EqualsTester() {
     this(new RelationshipTester.ItemReporter());
     this. = checkNotNull(itemReporter);

Adds equalityGroup with objects that are supposed to be equal to each other and not equal to any other equality groups added to this tester.
   public EqualsTester addEqualityGroup(Object... equalityGroup) {
     return this;

Run tests on equals method, throwing a failure on an invalid test
  public EqualsTester testEquals() {
        Equivalence.equals(), "Object#equals""Object#hashCode");
    for (List<Objectgroup : ) {
    for (int run = 0; run < run++) {
    return this;
  private void testItems() {
    for (Object item : Iterables.concat()) {
      assertTrue(item + " must not be Object#equals to null", !item.equals(null));
      assertTrue(item + " must not be Object#equals to an arbitrary object of another class",
      assertEquals(item + " must be Object#equals to itself"itemitem);
      assertEquals("the Object#hashCode of " + item + " must be consistent",
          item.hashCode(), item.hashCode());

Class used to test whether equals() correctly handles an instance of an incompatible class. Since it is a private inner class, the invoker can never pass in an instance to the tester
  private enum NotAnInstance {
New to GrepCode? Check out our FAQ X