public abstract class EqualsHashCodeTestCase
extends junit.framework.TestCase
equals
and hashCode
contract.
Override my createInstance
and
createNotEqualInstance
methods to
provide me with objects to test against. Both methods should return
objects that are of the same class.
WARNING: Extend me only if your class overrides
equals
to test for equivalence. If your class's
equals
tests for identity or preserves the behavior from
Object
, I'm not interested, because I expect
createInstance
to return equivalent but distinct objects.
Object.equals(Object)
,
Object.hashCode()
Constructor and Description |
---|
EqualsHashCodeTestCase(java.lang.String name)
Creates a new test.
|
Modifier and Type | Method and Description |
---|---|
protected abstract java.lang.Object |
createInstance()
Creates and returns an instance of the class under test.
|
protected abstract java.lang.Object |
createNotEqualInstance()
Creates and returns an instance of the class under test.
|
protected void |
setUp()
Sets up the test fixture.
|
void |
testEqualsAgainstNewObject()
Tests whether
equals holds up against a new
Object (should always be false ). |
void |
testEqualsAgainstNull()
Tests whether
equals holds up against null . |
void |
testEqualsAgainstUnequalObjects()
Tests whether
equals holds up against objects that should
not compare equal. |
void |
testEqualsIsConsistentAcrossInvocations()
Tests whether
equals is consistent. |
void |
testEqualsIsReflexive()
Tests whether
equals is reflexive. |
void |
testEqualsIsSymmetricAndTransitive()
Tests whether
equals is symmetric and
transitive. |
void |
testHashCodeContract()
Tests the
hashCode contract. |
void |
testHashCodeIsConsistentAcrossInvocations()
Tests the consistency of
hashCode . |
assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertFalse, assertFalse, assertNotNull, assertNotNull, assertNotSame, assertNotSame, assertNull, assertNull, assertSame, assertSame, assertTrue, assertTrue, countTestCases, createResult, fail, fail, failNotEquals, failNotSame, failSame, format, getName, run, run, runBare, runTest, setName, tearDown, toString
public EqualsHashCodeTestCase(java.lang.String name)
name
- name of the testprotected abstract java.lang.Object createInstance() throws java.lang.Exception
java.lang.Exception
protected abstract java.lang.Object createNotEqualInstance() throws java.lang.Exception
createInstance
.java.lang.Exception
protected void setUp() throws java.lang.Exception
setUp
in class junit.framework.TestCase
java.lang.Exception
public final void testEqualsAgainstNewObject()
equals
holds up against a new
Object
(should always be false
).public final void testEqualsAgainstNull()
equals
holds up against null
.public final void testEqualsAgainstUnequalObjects()
equals
holds up against objects that should
not compare equal.public final void testEqualsIsConsistentAcrossInvocations()
equals
is consistent.public final void testEqualsIsReflexive()
equals
is reflexive.public final void testEqualsIsSymmetricAndTransitive()
equals
is symmetric and
transitive.public final void testHashCodeContract()
hashCode
contract.public final void testHashCodeIsConsistentAcrossInvocations()
hashCode
.Copyright © ${project.year} ${project.holder}. All Rights Reserved.