public class HashCodeMethodTester extends Object
Provides a means of testing the correctness of the hashCode logic implemented by a type, based solely on the provision of the type, with respect to:
The following is tested:
Use the tests provided by this class (namely, testHashCode()) to test a class that overrides
hashCode() and equals().
As an example, to test the hashCode logic implemented by a class called MyClass do the following:
HashCodeMethodTester tester = new HashCodeMethodTester(); tester.testHashCodeMethod(MyClass.class);
To test the hashCode logic implemented by a class called MyClass without a no-argument constructor do the following:
HashCodeMethodTester tester = new HashCodeMethodTester();
tester.testHashCodeMethod(new Factory<MyClass>() {
@Override
public MyClass create() {
MyClass result = new MyClass("TEST_NAME");
return result;
}
});
The Factory creates new logically equivalent instances of MyClass. MyClass has overridden
equals() and hashCode(). In the above example, there is only one property, name, which is
considered by MyClass's hashCode logic.
| Constructor and Description |
|---|
HashCodeMethodTester()
Prefer
BeanVerifier |
| Modifier and Type | Method and Description |
|---|---|
FactoryCollection |
getFactoryCollection()
Get the collection of test data Factories with which you can register new Factories for custom Data Types.
|
FactoryLookupStrategy |
getFactoryLookupStrategy()
Get the FactoryLookupStrategy, which provides a means of acquiring Factories.
|
RandomValueGenerator |
getRandomValueGenerator()
Get a RandomValueGenerator.
|
protected void |
testHashCodeConsistent(EquivalentFactory<?> factory)
Test that the hashCode logic implemented by the type the specified factory creates is consistent with the
consistent item of the hashCode contract.
|
void |
testHashCodeMethod(Class<?> clazz)
Test that the hashCode logic implemented by the specified type is correct by testing:
|
void |
testHashCodeMethod(EquivalentFactory<?> factory)
Test that the hashCode logic implemented by the type the specified factory creates is correct by testing:
|
protected void |
testHashCodesEqual(EquivalentFactory<?> factory)
Test that the hashCode logic implemented by the type the specified factory creates returns equal hashCodes for
logically equivalent objects.
|
public HashCodeMethodTester()
BeanVerifierpublic void testHashCodeMethod(EquivalentFactory<?> factory) throws IllegalArgumentException, AssertionError
Test that the hashCode logic implemented by the type the specified factory creates is correct by testing:
If the test fails, an AssertionError is thrown.
factory - An EquivalentFactory that creates non-null logically equivalent objects that will be used to test
whether the equals logic implemented by the type is correct. The factory must create logically
equivalent but different actual instances of the type upon each invocation of create() in
order for the test to be meaningful and correct.IllegalArgumentException - If the specified factory is deemed illegal. For example, if it is null, if it creates a
null object or if it creates objects that are not logically equivalent.AssertionError - If the test fails.public void testHashCodeMethod(Class<?> clazz) throws IllegalArgumentException, AssertionError
Test that the hashCode logic implemented by the specified type is correct by testing:
If the test fails, an AssertionError is thrown.
clazz - The type to test the equals logic of.IllegalArgumentException - If the specified clazz is deemed illegal. For example, if it is null.AssertionError - If the test fails.protected void testHashCodesEqual(EquivalentFactory<?> factory) throws IllegalArgumentException, AssertionError
Test that the hashCode logic implemented by the type the specified factory creates returns equal hashCodes for
logically equivalent objects.
If the test fails, an AssertionError is thrown.
factory - An EquivalentFactory that creates non-null logically equivalent objects that will be used to test
whether the hashCode logic implemented by the type returns equal hashCodes for logically equivalent
objects. The factory must create logically equivalent but different actual instances of the type upon
each invocation of create() in order for the test to be meaningful.IllegalArgumentException - If the specified factory is deemed illegal. For example, if it is null, if it creates a
null object or if it creates objects that are not logically equivalent.AssertionError - If the test fails.protected void testHashCodeConsistent(EquivalentFactory<?> factory) throws IllegalArgumentException, AssertionError
Test that the hashCode logic implemented by the type the specified factory creates is consistent with the
consistent item of the hashCode contract.
If the test fails, an AssertionError is thrown.
factory - An EquivalentFactory that creates non-null logically equivalent objects that will be used to test
whether the hashCode logic implemented by the type is consistent with the consistent item of the
hashCode contract. The factory must create logically equivalent but different actual instances of the
type upon each invocation of create() in order for the test to be meaningful.IllegalArgumentException - If the specified factory is deemed illegal. For example, if it is null or if it creates
a null object.AssertionError - If the test fails.public RandomValueGenerator getRandomValueGenerator()
public FactoryCollection getFactoryCollection()
public FactoryLookupStrategy getFactoryLookupStrategy()
Copyright © 2010–2020 meanbean. All rights reserved.