
179
over these indirect inputs. We can do so by using a Test Stub that returns the
value that should drive the SUT into the desired code path. As part of fi xture
setup, we must force the SUT to use the stub instead of the real component.
The Test Stub can be built two ways: as a Hard-Coded Test Stub (see Test
Stub), which contains hand-written code that returns the specifi c values, or
as a Confi gurable Test Stub (see Test Stub), which is confi gured by the test to
return the desired values. In both cases, the SUT must use the Test Stub instead
of the real component.
Many of these alternative paths result in “successful” outputs from the SUT;
these tests are considered Simple Success Tests and use a style of Test Stub called
a Responder (see Test Stub). Other paths are expected to raise errors or excep-
tions; they are considered Expected Exception Tests (see Test Method) and use
a style of stub called a Saboteur (see Test Stub).
Making Tests Repeatable and Robust
The act of replacing a real depended-on component (DOC) with a Test Stub has
a very desirable side effect: It makes our tests both more robust and more repeat-
able.
2
By using a Test Stub, we replace a possibly nondeterministic component
with one that is completely deterministic and under test control. This is a good
example of the Isolate the SUT principle (see page 43).
Verify Indirect Output Behavior
Thus far we have focused on getting control of the indirect inputs of the SUT
and verifying readily visible direct outputs by inspecting the post-state test of the
SUT. This kind of result verifi cation is known as State Verifi cation (page 462).
Sometimes, however, we cannot confi rm that the SUT has behaved correctly
simply by looking at the post-test state. That is, we may still have some Untested
Requirements (see Production Bugs) that can only be verifi ed by doing Behavior
Verifi cation (page 468).
We can build on what we already know how to do by using one of the close
relatives of the Test Stub to intercept the outgoing method calls from our SUT.
A Test Spy “remembers” how it was called so that the test can later retrieve the
usage information and use Assertion Method calls to compare it to the expected
usage. A Mock Object can be loaded with expectations during fi xture setup,
which it subsequently compares with the actual calls as they occur while the
SUT is being exercised.
2
See Robust Test (see page 29) and Repeatable Test (see page 26) for a more detailed
description.
Roadmap to Highly Maintainable Automated Tests