Java Reference
In-Depth Information
method contract pretty much makes it impossible to compare a subclass with a superclass
correctly, so we now use class equality (see Chapter 23, Reflection, or “A Class Named
Class” for details on the class descriptor).
Here is a basic JUnit test (see Avoiding the Need for Debuggers with Unit Testing ) for the
EqualsDemo class:
/** Some JUnit test cases for EqualsDemo.
* Writing a full set is left as "an exercise for the reader".
*/
public
public class
class EqualsDemoTest
EqualsDemoTest {
/** an object being tested */
EqualsDemo d1 ;
/** another object being tested */
EqualsDemo d2 ;
/** Method to be invoked before each test method */
@Before
public
public void
void setUp () {
d1 = new
new EqualsDemo ();
d2 = new
new EqualsDemo ();
}
@Test
public
public void
void testSymmetry () {
assertTrue ( d1 . equals ( d1 ));
}
@Test
public
public void
void testSymmetric () {
assertTrue ( d1 . equals ( d2 ) && d2 . equals ( d1 ));
}
@Test
public
public void
void testCaution () {
assertTrue (! d1 . equals ( null
null ));
}
}
With all that testing, what could go wrong? Well, some things still need care. What if the ob-
ject is a subclass of EqualsDemo ? We should test that it returns false in this case.
Search WWH ::




Custom Search