Java Reference
In-Depth Information
}
}
The output of this test program is:
11.25
Expected: 11.25
Alternatively, if you use a program that lets you test objects interactively, such as
BlueJ, construct an object and apply the method calls.
101
102
3.6 Unit Testing
In the preceding section, we completed the implementation of the BankAccount
class. What can you do with it? Of course, you can compile the file
BankAccount.java . However, you can't execute the resulting
BankAccount.class file. It doesn't contain a main method. That is normalȌ
most classes don't contain a main method.
A unit test verifies that a class works correctly in isolation, outside a complete
program.
In the long run, your class may become a part of a larger program that interacts with
users, stores data in files, and so on. However, before integrating a class into a
program, it is always a good idea to test it in isolation. Testing in isolation, outside a
complete program, is called unit testing.
To test your class, you have two choices. Some interactive development
environments have commands for constructing objects and invoking methods (see
Advanced Topic 2.1 ). Then you can test a class simply by constructing an object,
calling methods, and verifying that you get the expected return values. Figure 6
shows the result of calling the getBalance method on a BankAccount object in
BlueJ.
Alternatively, you can write a tester class. A tester class is a class with a main
method that contains statements to run methods of another class. A tester class
typically carries out the following steps:
Search WWH ::




Custom Search