Information Technology Reference
In-Depth Information
LISTING 6-13
Sample Sunny Day Test Case
public void testFindVerifyDefinition() throws Exception{
final WordDAOImpl dao = new WordDAOImpl();
final IWord wrd = dao.findWord("pugnacious");
for(Iterator iter = wrd.getDefinitions().iterator();
iter.hasNext();){
IDefinition def = (IDefinition)iter.next();
TestCase.assertEquals(
"def is Combative in nature; belligerent.",
"Combative in nature; belligerent.",
def.getDefinition());
}
}
During functional testing of the larger application (in this case, a
dictionary), it is discovered that if the user attempts to search for a word
that isn't in the dictionary, the application heaves a nasty exception
stack trace, which utterly confuses users. After some crafty detective
work, someone discovers that the findWord method in WordDAOImpl
throws an unexpected IndexOutOfBoundsException (which is masked
by a FindException ) if no word is returned via the Hibernate API.
This aberrant behavior wasn't accounted for! A defect has been
discovered! All is not lost, though. Remember, we are forgiven for cre-
ating this defect, but only once . We have an opportunity to fix this
nefarious glitch, but if it breaks again we should rethink our approach.
The first step in regaining your pride is to write a test case that
exposes the defect. Read that sentence again slowly. Your first reaction
may be to fix the offending code and move on to other, more exciting
things (happy hour!); however, if you go that route, you lose an excel-
lent chance to ensure that the same bug never comes back again. Start
by writing a test case that triggers the same exact behavior that was
reported in the defect summary. In this case, we need to cause the code
to throw an IndexOutOfBoundsException , such as the one shown in
Listing 6-14. Remember that we're writing a test to pass on the behav-
ior, not to fail.
Test Case Verifying the Defect
LISTING 6-14
public void testFindInvalidWord() throws Exception{
final WordDAOImpl dao = new WordDAOImpl();
try{
final IWord wrd = dao.findWord("fetit");
Search WWH ::




Custom Search