106 CHAPTER 5GENERICS,COLLECTIONS, AND TESTING
assertEquals(expectedValue, actualValue)
assertEquals(messageString, expectedValue, actualValue)
assertFalse(booleanCondition)
assertFalse(messageString, booleanCondition)
assertNotNull(object)
assertNotNull(messageString, object)
assertNotSame(expectedValue, actualValue)
assertNotSame(messageString, expectedValue, actualValue)
assertNull(object)
assertNull(messageString, object)
assertSame(expectedValue, actualValue)
assertSame(messageString, expectedValue, actualValue)
assertTrue(booleanCondition)
assertTrue(messageString, booleanCondition)
failNotEquals(messageString, expectedValue, actualValue)
failNotSame(messageString, expectedValue, actualValue)
FIGURE 5.12
Assertion methods in JUnit.
the DUMMYSTRING and DUMMYINT variables to be both private and static. In order
to use them as class variables in the testConstructor method in Figure 5.13, we
had to change the declaration to public. We note that we have not explicitly tested
the getName, getOffice, getPhone,andgetTeaching methods. We have, however,
used each of these in testing other methods, so it can be argued that these methods
have been tested implicitly.
Further thought together with a look at the code for testing the equals,
readRecord,andtoString methods shows that testing is an art, not entirely a
science. How much testing is enough? What should be tested in which test method?
For example, in testing the equals method, we have not tested (once again) the
fact that our constructor works correctly, but we have included lines to verify that
the setName method worked as intended. If the test methods are ever intended
to be broken apart and used separately, they should test all aspects of the code,
but if the test class is intended only to be used as a single class, then we could
assume, for example, that the setName method has been tested elsewhere and does
not need further testing in the testEquals method. (This does, of course, lead to
the possibility that the test methods will begin by being treated as a block and
then some methods will be yanked out for use in another testing class. Such actions
cannot be predicted, and such improper later use of code cannot be prevented. The
prudent programmer,
6
though, will include in the documentation of a test method
a sufficient number of caveats about what is and what is not tested so as to avoid
being blamed later for a poor test method.)
6
Those who are unfamiliar with this sort of expression should look up “prudent mariner” in a
nautical context.