Java Reference
In-Depth Information
3. Check that the constructor of the class works properly. This means check-
ing whether the fields of each new instance were properly initialized.
Function toString can be useful in testing. If toString has been writ-
ten, it is easy to print the contents of a class instance using it:
System.out.println(x.toString());
This test will also help test method toString .
4. Test all the other methods. This testing can follow the pattern described
earlier.
When creating a subclass of TestCases to use with JUnit, a procedure
setup (see Sec. 2.4 of Appendix I) can be written that creates instances of the
class being tested and stores them (i.e. their names) in fields of the subclass.
These fields can then be used in the test procedures of the subclass.
14.2
Approaches to creating test cases
In Sec. 14.1, we developed test cases for a method by looking only at the speci-
fication of the method —even before the method body was written. This kind of
test-case development is known as blackbox , or functional , testing. There are
other ways to go about developing test cases. We list three main ones here.
1. Exhaustive testing . Exhaustive testing, or testing a program on all possi-
ble inputs, sounds good but is generally infeasible for most programs
/** = name of 10 * n , for 2≤n<10 , e.g. tensName(3) is "thirty"*/
public static String tensName( int n) {
if (n == 2)
{ return "twenty "; }
if (n == 3)
{ return "thirty "; }
if (n == 4)
{ return "fourty "; }
if (n == 5)
{ return "fifty "; }
if (n == 6)
{ return "sixty "; }
if (n == 7)
{ return "seventy "; }
if (n == 8)
{ return "eighty "; }
return "ninety ";
}
Figure 14.3:
Function tensName , with an error
Search WWH ::




Custom Search