9780132855624.pdf by Unknown

9780132855624.pdf by Unknown

Author:Unknown
Language: eng
Format: epub
Published: 2011-10-15T08:40:51+00:00


7.12 Summary

|

265

7.10

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 investigate 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 interactive 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.

7.11

Putting the techniques into practice

This chapter has described several techniques that can be used either to understand a new program 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?

7.12

Summary

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 266

| Chapter 7 ■ Well-behaved objects

methods and classes. We have also looked at some basic techniques for automating the testing process and performing simple debugging.

Writing good JUnit tests for our classes ensures that errors are detected early, and they give a good indication which part of the system an error originates in, making the resulting debugging task much easier.

Terms introduced in this chapter

syntax error,



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.