public final class EqualsTester
extends java.lang.Object
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:
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.
Modifier and Type | Class and Description |
---|---|
private static class |
EqualsTester.NotAnInstance
Class used to test whether equals() correctly handles an instance of an incompatible class.
|
Modifier and Type | Field and Description |
---|---|
private java.util.List<java.util.List<java.lang.Object>> |
equalityGroups |
private RelationshipTester.ItemReporter |
itemReporter |
private static int |
REPETITIONS |
Constructor and Description |
---|
EqualsTester()
Constructs an empty EqualsTester instance
|
EqualsTester(RelationshipTester.ItemReporter itemReporter) |
Modifier and Type | Method and Description |
---|---|
EqualsTester |
addEqualityGroup(java.lang.Object... equalityGroup)
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. |
EqualsTester |
testEquals()
Run tests on equals method, throwing a failure on an invalid test
|
private void |
testItems() |
private static final int REPETITIONS
private final java.util.List<java.util.List<java.lang.Object>> equalityGroups
private final RelationshipTester.ItemReporter itemReporter
public EqualsTester()
EqualsTester(RelationshipTester.ItemReporter itemReporter)
public EqualsTester addEqualityGroup(java.lang.Object... equalityGroup)
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 testEquals()
private void testItems()