Java Reference
In-Depth Information
found, but we do not know whether the errors are in the tester or the engine. So the first step
is to check that the tester appears to be using the engine appropriately.
We note that the first statement of testPlus assumes that the engine field already refers
to a valid object:
We can verify that this is the case by checking the tester's constructor. It is a common
error for an object's fields not to have been initialized properly, either in their declara-
tions or in a constructor. If we attempt to use a field with no associated object, then a
NullPointerException is a likely runtime error.
The first statement's call to clear appears to be an attempt to put the calculator engine into
a valid starting state, ready to receive instructions to perform a calculation. This looks like
a reasonable thing to do, equivalent to pressing a “reset” or “clear” key on a real calculator.
At this stage, we do not look at the engine class to check exactly what the clear method
does. That can wait until we have achieved a level of confidence that the tester's actions are
reasonable. Instead, we simply make a penciled note to check that clear puts the engine
into a valid starting state as expected.
The next statement in testPlus is the entry of a digit via the numberPressed method:
This too is reasonable, as the first step in making a calculation is to enter the first operand.
Once again, we do not look to see what the engine does with the number. We simply assume
that it stores it somewhere for later use in the calculation.
The next statement calls plus , so we now know that the full value of the left operand is 3. We
could make a penciled note of this fact on the printout, or make a tick against this assertion in
one of the comments of testPlus . Similarly, we should note or confirm that the operation be-
ing executed is addition. This seems like a trivial thing to do, but it is all too easy for a class's
comments to get out of step with the code they are supposed to document. So checking the
comments at the same time as we read the code can help us avoid being misled by them later.
Next, another single digit is entered as the right operand by a further call to numberPressed .
Completion of the addition is requested by a call to the equals method. We might make
a penciled note that, from the way it has been used in testPlus , the equals method ap-
pears not to return the result of the calculation, as we might have expected otherwise. This is
something else that we can check when we look at CalcEngine .
The final statement of testPlus obtains the value that should appear in the calculator's
return engine.getDisplayValue();
Presumably, this is the result of the addition, but we cannot know that for sure without look-
ing in detail at CalcEngine . Once again, we shall make a note to check that this is indeed
the case.
With our examination of testPlus completed, we have gained a reasonable degree of con-
fidence that it uses the engine appropriately: that is, simulating a recognizable sequence of
Search WWH ::

Custom Search