
560
Chapter 23 Test Double Patterns
Variation: Confi guration Interface
A Confi guration Interface comprises a separate set of methods that the
Confi gurable Test Double provides specifi cally for use by the test to set each
value that the Confi gurable Test Double returns or expects to receive. The
test simply calls these methods during the fi xture setup phase of the Four-Phase
Test (page 358). The SUT uses the “other” methods on the Confi gurable Test
Double (the “normal” interface). It isn’t aware that the Confi guration Interface
exists on the object to which it is delegating.
Confi guration Interfaces come in two fl avors. Early toolkits, such as Mock-
Maker, generated a distinct method for each value we needed to confi gure. The
collection of these setter methods made up the Confi guration Interface. More
recently introduced toolkits, such as JMock, provide a generic interface that is used
to build an Expected Behavior Specifi cation (see Behavior Verifi cation on page
468) that the Confi gurable Test Double interprets at runtime. A well-designed
fl uent interface can make the test much easier to read and understand.
Variation: Confi guration Mode
We can avoid defi ning a separate set of methods to confi gure the Test Double by
providing a Confi guration Mode that the test uses to “teach” the Confi gurable
Test Double what to expect. At fi rst glance, this means of confi guring the Test
Double can be confusing: Why does the Test Method (page 348) call the methods
of this other object before it calls the methods it is exercising on the SUT? When
we come to grips with the fact that we are doing a form of “record and play-
back,” this technique makes a bit more sense.
The main advantage of using a Confi guration Mode is that it avoids creating
a separate set of methods for confi guring the Confi gurable Test Double because
we reuse the same methods that the SUT will be calling. (We do have to pro-
vide a way to set the values to be returned by the methods, so we have at least
one additional method to add.) On the fl ip side, each method that the SUT is
expected to call now has two code paths through it: one for the Confi guration
Mode and another for the “usage mode.”
Variation: Hand-Built Test Double
A Hand-Built Test Double is one that was defi ned by the test automater for one
or more specifi c tests. A Hard-Coded Test Double is inherently a Hand-Built Test
Double, while a Confi gurable Test Double can be either hand-built or gener-
ated. This book uses Hand-Built Test Doubles in a lot of the examples because
it is easier to see what is going on when we have actual, simple, concrete code to
look at. This is the main advantage of using a Hand-Built Test Double; indeed,
Confi gurable
Test Double