Package com.google.common.testing
Class EqualsTester
- java.lang.Object
-
- com.google.common.testing.EqualsTester
-
public final class EqualsTester extends java.lang.Object
Tester for equals() and hashCode() methods of a class.The simplest use case is:
new EqualsTester().addEqualityGroup(foo).testEquals();
This tests
foo.equals(foo)
,foo.equals(null)
, and a few other operations.For more extensive testing, add multiple equality groups. Each group should contain objects that are equal to each other but unequal to the objects in any other group. For example:
new EqualsTester() .addEqualityGroup(new User("page"), new User("page")) .addEqualityGroup(new User("sergey")) .testEquals();
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.
- Since:
- 10.0
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private static class
EqualsTester.NotAnInstance
Class used to test whether equals() correctly handles an instance of an incompatible class.
-
Field Summary
Fields Modifier and Type Field Description private java.util.List<java.util.List<java.lang.Object>>
equalityGroups
private RelationshipTester.ItemReporter
itemReporter
private static int
REPETITIONS
-
Constructor Summary
Constructors Constructor Description EqualsTester()
Constructs an empty EqualsTester instanceEqualsTester(RelationshipTester.ItemReporter itemReporter)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description EqualsTester
addEqualityGroup(java.lang.Object... equalityGroup)
AddsequalityGroup
with objects that are supposed to be equal to each other and not equal to any other equality groups added to this tester.EqualsTester
testEquals()
Run tests on equals method, throwing a failure on an invalid testprivate void
testItems()
-
-
-
Field Detail
-
REPETITIONS
private static final int REPETITIONS
- See Also:
- Constant Field Values
-
equalityGroups
private final java.util.List<java.util.List<java.lang.Object>> equalityGroups
-
itemReporter
private final RelationshipTester.ItemReporter itemReporter
-
-
Constructor Detail
-
EqualsTester
public EqualsTester()
Constructs an empty EqualsTester instance
-
EqualsTester
EqualsTester(RelationshipTester.ItemReporter itemReporter)
-
-
Method Detail
-
addEqualityGroup
public EqualsTester addEqualityGroup(java.lang.Object... equalityGroup)
AddsequalityGroup
with objects that are supposed to be equal to each other and not equal to any other equality groups added to this tester.
-
testEquals
public EqualsTester testEquals()
Run tests on equals method, throwing a failure on an invalid test
-
testItems
private void testItems()
-
-