Java Reference
In-Depth Information
procedure named test... with no parameters for each test case, and the body of
each procedure contains one call to a method of class TestCase . We have writ-
ten independent methods for the test cases because the test cases are independ-
ent —one does not depend on another one working correctly.
This is one context where we do not write method specifications. Here, the
context is so specific, the bodies of the methods so simple, and the names of the
methods so descriptive that writing a specification for the methods would add lit-
tle. In this context, we know implicitly what each procedure does: it exercises a
test case.
Testing a class
Testing a class is harder than testing a method, for a class generally contains
many methods. The test driver should:
1. Declare at least one and perhaps more variables with the class as their
2. Create instances of the class and assign them to the variables.
import junit.framework.TestCase;
public class TestNumberOfVowels extends TestCase {
public void testDuplicateVowels()
{ assertEquals(7, numberOfVowels("This sentence has vowels.")); }
public void testEmpty()
{ assertEquals(0, numberOfVowels("")); }
public void testNoVowels()
{ assertEquals(0, numberOfVowels("bcd")); }
public void testOneVowel()
{ assertEquals(1, numberOfVowels("a")); }
public void testOneVowelSeveralLetters()
{ assertEquals(1, numberOfVowels("bad")); }
public void testEveryVowelNoConsonants()
{ assertEquals(5, numberOfVowels("aeiou")); }
public void testEveryVowelSeveralConsonants()
{ assertEquals(5, numberOfVowels("facetious")); }
public void testNull()
{ assertEquals(0, numberOfVowels( null )); }
Figure 14.2:
An instance of TestCase to test TestNumberOfVowels
Search WWH ::

Custom Search