Java Reference
In-Depth Information
Code 7.1
continued
The
SalesItem
class
if
(cents <= 9) {
return
"$"
+ dollars +
".0"
+ cents;
// zero padding
}
else
{
return
"$"
+ dollars +
"."
+ cents;
}
}
}
7.3.1
Using inspectors
When testing interactively, using object inspectors is often very helpful. In preparation for our
testing, create a
SalesItem
object on the object bench and open its inspector by selecting the
Inspect
function from the object's menu. Select the
comments
field and open its inspector as
well (Figure 7.1). Check that the list has been created (is not null) and is initially of size 0.
Check also that the size grows as you add comments. Leave the comment-list inspector open to
assist with subsequent tests.
Figure 7.1
Inspector for the
comments
list
An essential component of testing classes that use data structures is checking that they behave
properly both when the data structures are empty and—if appropriate—when they are full.
Testing for full data structures only applies to those that have a fixed limit, such as arrays. In
our case, where we use an
ArrayList
, testing for the list being full does not apply, because
the list expands as needed. However, making tests with an empty list is important, as this is a
special case that needs special treatment.
A first test that can be performed on
SalesItem
is to call its
showInfo
method before any comments
have been added. This should correctly show the item's description and price, and no comments.
A key feature of good testing is to ensure that
boundaries
are checked, because these are often
the points at which things go wrong. The boundaries associated with the
SalesItem
class are,
for example, the empty comment list. Boundaries set for the
Comment
class include the restric-
tion of the rating to the range 1 to 5. Ratings at the top and bottom of this range are boundary
Search WWH ::
Custom Search