Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
   * Copyright (C) 2011 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 relationships between groups of objects.

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

     .addEquivalenceGroup("hello", "h" + "ello")
     .addEquivalenceGroup("world", "wor" + "ld")

Note that testing java.lang.Object.equals(java.lang.Object) is more simply done using the EqualsTester. It includes an extra test against an instance of an arbitrary class without having to explicitly add another equivalence group.

Gregory Kick
10.0 TODO(gak): turn this into a test suite so that each test can fail independently
 @GwtCompatible public final class EquivalenceTester<T> {
   private static final int REPETITIONS = 3;
   private final Equivalence<? super T> equivalence;
   private final RelationshipTester<T> delegate;
   private final List<T> items = Lists.newArrayList();
   EquivalenceTester(final Equivalence<? super T> equivalence) {
     this. = checkNotNull(equivalence);
     this. = new RelationshipTester<T>(new RelationshipAssertion<T>() {
       @Override public void assertRelated(T item, T related) {
         assertTrue("$ITEM must be equivalent to $RELATED"equivalence.equivalent(itemrelated));
         int itemHash = equivalence.hash(item);
         int relatedHash = equivalence.hash(related);
         assertEquals("the hash (" + itemHash + ") of $ITEM must be equal to the hash ("
             + relatedHash + ") of $RELATED"itemHashrelatedHash);
       @Override public void assertUnrelated(T item, T unrelated) {
         assertTrue("$ITEM must be inequivalent to $UNRELATED",
   public static <T> EquivalenceTester<T> of(Equivalence<? super T> equivalence) {
     return new EquivalenceTester<T>(equivalence);

Adds a group of objects that are supposed to be equivalent to each other and not equivalent to objects in any other equivalence group added to this tester.
   public EquivalenceTester<T> addEquivalenceGroup(T first, T... rest) {
     return this;
  public EquivalenceTester<T> addEquivalenceGroup(Iterable<T> group) {
    return this;

Run tests on equivalence methods, throwing a failure on an invalid test
  public EquivalenceTester<T> test() {
    for (int run = 0; run < run++) {
    return this;
  private void testItems() {
    for (T item : ) {
      assertTrue(item + " must be inequivalent to null", !.equivalent(itemnull));
      assertTrue("null must be inequivalent to " + item, !.equivalent(nullitem));
      assertTrue(item + " must be equivalent to itself".equivalent(itemitem));
      assertEquals("the hash of " + item + " must be consistent".hash(item),
New to GrepCode? Check out our FAQ X