Skip to main content

more options

Part 6. The class definition    Part 8. API packages, etc.

Module 1, part 7. Specifications, testing, and debugging

Introduction

Part 7 is about testing. But before one can test, one has to know what to test for —one has to know what a method or class to be tested is supposed to do. So, we discuss briefly the specifications of methods, fields of a class, and the class itself.

Since you have programmed before, you will know much of what we say here already, but a review won't hurt, especially of a topic as important as testing and debugging. Also, the use of a JUnit tester will probably be new to you.

Contents

No. Topic Discussion
1. Specifications of methods.
(blecture doc ppt.pdf)
Reading: Gries/Gries, Sec.13.3.1, pp. 376–378.
Each method should have a precise specification, indicating (1) constraints on calls of the method and (2) what the method does.
2. Assertions in programs.
All 3 lectures on p. 1-6 of the ProgramLive CD.
An assertion is a true-false statement about the program variables that is placed somewhere in a program. By placing it in before or after a statement, one asserts that the assertion is true at that point.
3. Javadoc specifications.
(blecture doc)
Reading: Gries/Gries, App. II.2, pp. 496–497; App. I.2.3, p. 486.
Comments of a certain form, beginning with /**, will be extracted and put on a web page. This allows the user to look at the specs without having to look at the Java program.
4. Testing.
(html)
Testing is the process of running a program against "test cases" in order to get some evidence of the program's correctness. If a test case reveals an error, debugging (see points 6–7 below) takes place to find and correct the error.
5. JUnit testing.
(blecture doc)
DrJava provides a simple means for saving suites of test cases and executing them at will.
6. Debugging (introduction).
(html)
If an error has been detected, it must be found and removed. This is called debugging.
7. A demonstration of debugging.
Watch the two lectures on p. 14-4 of the ProgramLive CD (feedback)
We give some pointers on inserting print statements and demonstrate how to find a bug.
  Reading material for testing and debugging: Chapter 14 of Gries/Gries, pp. 385–401, is devoted to testing and debugging. If your previous course did not teach you much about these topics, read this chapter carefully.

Sec. 14.1 discusses the process of testing; Sec. 14.2, approaches to creating test cases; Sec. 14.4, Java's assert statement, which can be useful in debugging; and Sec. 14.5, debugging itself.

Gries/Gries, and App. I.2.4, pp. 486–489, covers JUnit testing in DrJava.

The ProgramLive CD, pp. 14–1 through 14–4 are especially useful.