Programming and Problem Solving with Java

(やまだぃちぅ) #1
6.7 Packages | 297

Package Example


If the class Phonemust be made available for others to use, we need to be sure that it has been
thoroughly tested. Let’s write and implement a test plan to test Phone. We must test five
methods plus the constructor. The knowledge methods need to be tested with only one case:
Do they return the proper fields? Likewise, do the two conversion methods return the data
in the proper format? The sixth method compares two instances of the class Phone. This
method must be tested several times: once when the area codes are not equal, once when
the area codes are equal but the numbers are not, and once when both are equal.


Reason for Test Case Input Values Expected Output Observed Output
Phone 523, 3733344
knowAreaCode 523
knowDigits 3733344
asDigits 5233733344
asString 523–373–3344
equal 523, 3774433 Numbers are not equal
521, 3733344 Numbers are not equal
523, 3733344 Numbers are equal

This test plan is implemented in the following application class:

importjava.io.*;
importPhone;
public classTestPhone
{
public static voidmain(String[] args) throwsIOException
{
Phone firstPhone;
Phone secondPhone;
Phone thirdPhone;
Phone fourthPhone;
PrintWriter outFile;
outFile = new PrintWriter(new FileWriter("phoneOut"));
outFile.println("Test results for class Phone");
firstPhone = new Phone(523, 3733344);
secondPhone = new Phone(523, 3774433);
thirdPhone = new Phone(521, 3733344);
fourthPhone = new Phone(523, 3733344);
outFile.println("knowAreaCode: "+ firstPhone.knowAreaCode());
outFile.println("knowDigits: "+ firstPhone.knowDigits());
outFile.println("asDigits: "+ firstPhone.asDigits());
outFile.println("asString: "+ firstPhone.asString());
outFile.print("equal: Is firstPhone equal to secondPhone? ");
if (firstPhone.equals(secondPhone))

Free download pdf