
70
Chapter 6 Test Automation Strategy
The “solution of last resort” is the Test Hook (page 709).
9
These constructs
do have utility as temporary measures that allow us to automate tests to act
as a Safety Net (page 24) while refactoring to retrofi t testability. We defi nitely
shouldn’t make a habit of using them, however, as continued use of Test Hooks
will result in Test Logic in Production (page 217).
Figure 6.10 A Test Double being “injected” into a SUT by a test. A test can
use Dependency Injection to replace a DOC with an appropriate Test Double. The
DOC is passed to the SUT by the test as or after it has been created.
A third kind of test worth mentioning is the asynchronous test, in which the test
interacts with the SUT through real messaging. Because the responses to these
requests also come asynchronously, these tests must include some kind of inter-
process synchronization such as calls to wait. Unfortunately, the need to wait for
message responses that might never arrive can cause these tests to take much,
much longer to execute. This style of testing should be avoided at all costs in
unit and component tests.
Fortunately, the Humble Executable pattern (see Humble Object on page 695)
can remove the need to conduct unit tests this way (Figure 6.11). It involves
putting the logic that handles the incoming message into a separate class or
component, which can then be tested synchronously using either a round-trip
or layer-crossing style.
A related issue is the testing of business logic through a UI. In general, such
Indirect Testing (see Obscure Test) is a bad idea because changes to the UI code
will break tests that are trying to verify the business logic behind it. Because the
UI tends to change frequently, especially on agile projects, this strategy will
greatly increase test maintenance costs. Another reason this is a bad idea is that
9
These typically take the form of if (testing) then ... else ... endif.
DOC
Test
Double
Creation
Creation
SUT
Usage
Client
Exercise
Setup
Exercise
Verify
Teardown
Usage Usage
DOC
Test
Double
Creation
Creation
SUT
Usage
Client
Exercise
Setup
Exercise
Verify
Teardown
Usage Usage