Java Reference
In-Depth Information
2.8 Implementing a Test Program
In this section, we discuss the steps that are necessary to implement a test program.
The purpose of a test program is to verify that one or more methods have been
implemented correctly. A test program calls methods and checks that they return the
expected results. Writing test programs is a very important activity. When you
implement your own methods, you should always supply programs to test them.
In this topic, we use a very simple format for test programs. You will now see such a
test program that tests a method in the Rectangle class. The program performs the
following steps:
1. Provide a tester class.
2. Supply a main method.
3. Inside the main method, construct one or more objects.
4. Apply methods to the objects.
5. Display the results of the method calls.
6. Display the values that you expect to get.
Whenever you write a program to test your own classes, you need to follow these steps
as well.
Our sample test program tests the behavior of the translate method. Here are the
key steps (which have been placed inside the main method of the
Rectangle-Tester class).
47
48
Rectangle box = new Rectangle(5, 10, 20, 30);
// Move the rectangle
box.translate(15, 25);
// Print information about the moved rectangle
System.out.print(Ðx: Ñ);
System.out.println(box.getX());
System.out.println(ÐExpected: 20Ñ);
Search WWH ::




Custom Search