Java Reference
In-Depth Information
TIP: (continued)
It is sometimes impossible or inconvenient to test a method without using some
other method that has not yet been written or has not yet been tested. In this case,
you can use a simplified version of the missing or untested method. These simpli-
fied methods are called stubs . These stubs will not necessarily perform the correct
calculation, but they will deliver values that suffice for testing, and they are simple
enough that you can have confidence in their performance. For example, the fol-
lowing is a possible stub:
stub
/**
Computes the probability of rain based on temperature, barometric pressure,
and relative humidity. Returns the probability as a fraction between
0 and 1.
*/
public double rainChance( double temperature,
double pressure, double humidity)
{
return 0.5;//Not correct but good enough for a stub.
}
The Fundamental Rule for Testing Methods
Every method should be tested in a program in which every other method in the testing pro-
gram has already been fully tested and debugged.
Self-Test Exercises
13. In the definition of precedes in Display 4.7, we used
month.equals(otherDate.month)
to test whether two months are equal; but we used
getMonth() < otherDate.getMonth()
to test whether one month comes before another. Why did we use month in one
case and getMonth in another case?
14. What is the fundamental rule for testing methods?
Search WWH ::




Custom Search