Notes
Outline
Chapter 8
Testing a class
This chapter discusses
Testing in general.
Testing a single class.
Test plans.
Building a test system.
Testing
Testing is an activity whose goal is to determine if the implementation is correct.
A successful test is one that reveals some previously undiscovered error.
Testing Phases
Test activities are determined and test data selected.
The test is conducted and test results are compared with expected results.
Test design
Begins with an analysis of:
The functional specifications of the system.
The ways in which the system will be used (referred to as “use cases”).
Testing based on these considerations is referred to as “black box testing.”
Black Box testing
The test designer ignores the internal structure of the implementation in developing the test.
The expected external behavior of the system drives the selection of test activities and data.
The system is treated as a “black box” whose behavior can be observed, but whose internal structure cannot.
Test case
Test cases are defined by:
A statement of case objectives.
The data set for the case.
The expected results.
Equivalency groups
Test cases are chosen from each equivalency group.
Particular attention is paid to data that lie on group “boundaries.”
Test plan
A test plan is a document that describes the test cases giving the purpose of each case, the data values to be used, and the expected results.
Test design should be carried out concurrently with system development.
Developing and refining test cases based on the implementation of the system is referred to as “white box testing.”
A test plan can be based on class specifications only.
Test system
Test systems allow us to interact with the object we want to test.
The test system will let us create the object to be tested and then act as a client of the object.
Testing Counter
Class specifications:
public class Counter
A simple integer counter.
public Counter ()
Create a new Counter initialized to 0.
public int count ()
The current count.
ensure:
this.count() >= 0
public void reset ()
Reset this Counter to 0.
public void increment ()
Increment the count by 1.
public void decrement ()
Decrement the count by 1. If the count is 0, it is not decremented.
Possible system
Enter number denoting action to perform:
Increment..............1
Decrement..............2
Reset..................3
Create a new Counter...4
Exit...................5
Enter choice.
1
The current count is: 1
Enter number denoting action to perform:
Increment..............1
Decrement..............2
Reset..................3
Create a new Counter...4
Exit...................5
Enter choice.
1
The current count is: 2 ...
Building a test system
The test system will be composed of two objects:
A Counter to be tested.
A test driver to invoke the Counter’s methods.
The test driver prompts the user with a menu, gets input from the user, and provides output in the form of the count.
CounterTUI
TUI -> “Textual User Interface.”
Basic input/output
Basic I/O consists of reading from standard input (keyboard) and writing to standard output (monitor).
Basic input/output (cont.)
We will use methods provided by the authors (OOJ.basicIO.BasicFileWriter, OOJ.basicIO.BasicFileReader).
public BasicFileWriter ()
Create a BasicFileWriter attached to standard output.
public void displayLine (String line)
Write the specified line to standard output.
public BasicFileReader ()
  Create a BasicFileReader attached to standard input.
public void readInt()
Reads a new int from standard input.
public int lastInt()
Returns the int read by readInt().
public void readline ()
Read rest of line from standard input.
CounterTUI specifications
public class CounterTUI
A text-based test driver for the class Counter.
public CounterTUI ()
Create a new test driver with a Counter to test.
public void start ()
Conduct the test.
CounterTUI implementation
public class CounterTUI {
public CounterTUI () {
  input = new
  OOJ.basicIO.BasicFileReader();
  output = new       OOJ.basicIO.BasicFileWriter();
  counter = new Counter();
  …
}
…
private OOJ.basicIO.BasicFileReader
  input;
private OOJ.basicIO.BasicFileWriter
  output;
private Counter counter;
}
CounterTUI implementation (cont.)
If we look at what happens when the test is conducted, we see that the following sequence of actions are repeated over and over:
Display menu to the user and prompt the user for input.
Get the user’s input.
Perform requested action.
Display results.
CounterTUI implementation (cont.)
We define an instance variable to hold the user’s choice.
private int choice; // the user’s most   // recent choice
The local methods we will create are specified as follows.
private void displayMenuPrompt ()
Display the menu to the user.
private void getChoice ()
Get the user’s choice.
private void executeChoice ()
Perform the action indicated by this.choice, and display the results
to the user.
While loop
A while loop continues to perform an action as long as a condition is true.
The while statement is composed of a condition (a boolean expression) and another statement, the body.
syntax:
while ( condition )
body
Slide 22
Slide 23
Slide 24
Implementation
main method: the top-level method that initiates execution of a system.
Although the main method is defined in a class, it is really outside the object-oriented paradigm.
It’s only purpose should be to create the top level objects and get the system started.
Implementation (cont.)
/**
* A test system for the class Counter.
*/
public class CounterTest {
/**
* Create the user interface, start
* the system.
*/
public static void main (String[] argv) {
  CounterTUI theInterface = new
CounterTUI();
  theInterface.start();
}
}
Test plan
action   expected     comment
increment   1            increment from initial state
increment   2     sequence of increments
increment   3
increment   4
decrement   3               sequence of decrements
decrement   2
increment   3        increment follows decrement
reset   0
increment   1      increment follows reset
decrement   0
decrement   0             decrement 0 count
reset   0
decrement   0     decrement follows reset
create   0     initial state
decrement   0           decrement from initial state
We’ve covered
The process of testing.
Test systems.
Text-based user interface.
“read-process-write” loop.
while loops.
main method.
Glossary