Java Reference
In-Depth Information
Choosing a debugging strategy
We have seen that several different debugging and testing strategies exist: written and verbal
walkthroughs, use of print statements (either temporary or permanent, with enabling switches),
interactive testing using the object bench, writing your own test class, and using a dedicated
unit test class.
In practice, we would use different strategies at different times. Walkthroughs, print statements,
and interactive testing are useful techniques for initial testing of newly written code, to inves-
tigate how a program segment works, or for debugging. Their advantage is that they are quick
and easy to use, they work in any programming language, and they are (except for the interac-
tive testing) independent of the environment. Their main disadvantage is that the activities are
not easily repeatable. This is okay for debugging, but for testing we need something better: we
need a mechanism that allows easy repetition for regression testing. Using unit test classes has
the advantage—once they have been set up—that tests can be replayed any number of times.
So Hacker's way of testing—writing his own test class—was one step in the right direction, but
was, of course, flawed. We know now that his problem was that although his class contained
reasonable method calls for testing, it did not include any assertions on the method results, and
thus did not detect test failure. Using a dedicated unit test class can solve these problems.
Exercise 7.36 Open your project again and add better testing by replacing Hacker's test
class with a unit test class attached to the CalcEngine . Add similar tests to those Hacker
used (and any others you find useful), and include correct assertions.
Putting the techniques into practice
This chapter has described several techniques that can be used either to understand a new pro-
gram or to test for errors in a program. The bricks project provides a chance for you to try out
those techniques with a new scenario. The project contains part of an application for a company
producing bricks. Bricks are delivered to customers on pallets (stacks of bricks). The Pallet
class provides methods telling the height and weight of an individual pallet, according to the
number of bricks on it.
Exercise 7.37 Open the bricks project. Test it. There are at least four errors in this project.
See if you can find them and fix them. What techniques did you use to find the errors? Which
technique was most useful?
When writing software, we should anticipate that it will contain logical errors. Therefore, it
is essential to consider both testing and debugging to be normal activities within the overall
development process. BlueJ is particularly good at supporting interactive unit testing of both
Search WWH ::

Custom Search