- Test Double
- Test Stub
- Test Spy
- Mock Object
- Fake Object
- Configurable Test Double
- Hard-Coded Test Double
- Test-Specific Subclass
Configurable Test Double
How do we tell a Test Double what to return or expect?
We configure a reusable Test Double with the values to be returned or verified during the fixture setup phase of a test.
Some tests require unique values to be fed into the SUT as indirect inputs or to be verified as indirect outputs of the SUT. This approach typically requires the use of Test Doubles as the conduit between the test and the SUT; at the same time, the Test Double somehow needs to be told which values to return or verify.
A Configurable Test Double is a way to reduce Test Code Duplication by reusing a Test Double in many tests. The key to its use is to configure the Test Double’s values to be returned or expected at runtime.
How It Works
The Test Double is built with instance variables that hold the values to be returned to the SUT or to serve as the expected values of arguments to method calls. The test initializes these variables during the setup phase of the test by calling the appropriate methods on the Test Double’s interface. When the SUT calls the methods on the Test Double, the Test Double uses the contents of the appropriate variable as the value to return or as the expected value in assertions.
When to Use It
We can use a Configurable Test Double whenever we need similar but slightly different behavior in several tests that depend on Test Doubles and we want to avoid Test Code Duplication or Obscure Tests —in the latter case, we need to see what values the Test Double is using as we read the test. If we expect only a single usage of a Test Double, we can consider using a Hard-Coded Test Double if the extra effort and complexity of building a Configurable Test Double are not warranted.
Implementation Notes
A Test Double is a Configurable Test Double because it needs to provide a way for the tests to configure it with values to return and/or method arguments to expect. Configurable Test Stubs and Test Spies simply require a way to configure the responses to calls on their methods; configurable Mock Objects also require a way to configure their expectations (which methods should be called and with which arguments).
Configurable Test Doubles may be built in many ways. Deciding on a particular implementation involves making two relatively independent decisions: (1) how the Configurable Test Double will be configured and (2) how the Configurable Test Double will be coded.
There are two common ways to configure a Configurable Test Double. The most popular approach is to provide a Configuration Interface that is used only by the test to configure the values to be returned as indirect inputs and the expected values of the indirect outputs. Alternatively, we may build the Configurable Test Double with two modes. The Configuration Mode is used during fixture setup to install the indirect inputs and expected indirect outputs by calling the methods of the Configurable Test Double with the expected arguments. Before the Configurable Test Double is installed, it is put into the normal (“usage” or “playback”) mode.
The obvious way to build a Configurable Test Double is to create a Hand-Built Test Double. If we are lucky, however, someone will have already built a tool to generate a Configurable Test Double for us. Test Double generators come in two flavors: code generators and tools that fabricate the object at runtime. Developers have built several generations of “mocking” tools, and several of these have been ported to other programming languages; check out http://xprogramming.com to see what is available in your programming language of choice. If the answer is “nothing,” you can hand-code the Test Double yourself, although this does take somewhat more effort.
Variation: Configuration Interface
A Configuration Interface comprises a separate set of methods that the Configurable Test Double provides specifically for use by the test to set each value that the Configurable Test Double returns or expects to receive. The test simply calls these methods during the fixture setup phase of the Four-Phase Test. The SUT uses the “other” methods on the Configurable Test Double (the “normal” interface). It isn’t aware that the Configuration Interface exists on the object to which it is delegating.
Configuration Interfaces come in two flavors. Early toolkits, such as MockMaker, generated a distinct method for each value we needed to configure. The collection of these setter methods made up the Configuration Interface. More recently introduced toolkits, such as JMock, provide a generic interface that is used to build an Expected Behavior Specification (see Behavior Verification) that the Configurable Test Double interprets at runtime. A well-designed fluent interface can make the test much easier to read and understand.
Variation: Configuration Mode
We can avoid defining a separate set of methods to configure the Test Double by providing a Configuration Mode that the test uses to “teach” the Configurable Test Double what to expect. At first glance, this means of configuring the Test Double can be confusing: Why does the Test Method 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 playback,” this technique makes a bit more sense.
The main advantage of using a Configuration Mode is that it avoids creating a separate set of methods for configuring the Configurable Test Double because we reuse the same methods that the SUT will be calling. (We do have to provide a way to set the values to be returned by the methods, so we have at least one additional method to add.) On the flip side, each method that the SUT is expected to call now has two code paths through it: one for the Configuration Mode and another for the “usage mode.”
Variation: Hand-Built Test Double
A Hand-Built Test Double is one that was defined by the test automater for one or more specific tests. A Hard-Coded Test Double is inherently a Hand-Built Test Double, while a Configurable Test Double can be either hand-built or generated. 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, some people consider this benefit to be so important that they use Hand-Built Test Doubles exclusively. We may also use a Hand-Built Test Double when no third-party toolkits are available or if we are prevented from using those tools by project or corporate policy.
Variation: Statically Generated Test Double
The early third-party toolkits used code generators to create the code for Statically Generated Test Doubles. The code is then compiled and linked with our handwritten test code. Typically, we will store the code in a source code repository [SCM]. Whenever the interface of the target class changes, of course, we must regenerate the code for our Statically Generated Test Doubles. It may be advantageous to include this step as part of the automated build script to ensure that it really does happen whenever the interface changes.
Instantiating a Statically Generated Test Double is the same as instantiating a Hand-Built Test Double. That is, we use the name of the generated class to construct the Configurable Test Double.
An interesting problem arises during refactoring. Suppose we change the interface of the class we are replacing by adding an argument to one of the methods. Should we then refactor the generated code? Or should we regenerate the Statically Generated Test Double after the code it replaces has been refactored? With modern refactoring tools, it may seem easier to refactor the generated code and the tests that use it in a single step; this strategy, however, may leave the Statically Generated Test Double without argument verification logic or variables for the new parameter. Therefore, we should regenerate the Statically Generated Test Double after the refactoring is finished to ensure that the refactored Statically Generated Test Double works properly and can be recreated by the code generator.
Variation: Dynamically Generated Test Double
Newer third-party toolkits generate Configurable Test Doubles at runtime by using the reflection capabilities of the programming language to examine a class or interface and build an object that is capable of understanding all calls to its methods. These Configurable Test Doubles may interpret the behavior specification at runtime or they may generate executable code; nevertheless, there is no source code for us to generate and maintain or regenerate. The down side is simply that there is no code to look at—but that really isn’t a disadvantage unless we are particularly suspicious or paranoid.
Most of today’s tools generate Mock Objects because they are the most fashionable and widely used options. We can still use these objects as Test Stubs, however, because they do provide a way of setting the value to be returned when a particular method is called. If we aren’t particularly interested in verifying the methods being called or the arguments passed to them, most toolkits provide a way to specify “don’t care” arguments. Given that most toolkits generate Mock Objects, they typically don’t provide a Retrieval Interface (see Test Spy).
Motivating Example
Here’s a test that uses a Hard-Coded Test Double to give it control over the time:
public void testDisplayCurrentTime_AtMidnight_HCM() throws Exception { // Fixture Setup // Instantiate hard-code Test Stub: TimeProvider testStub = new MidnightTimeProvider(); // Instantiate SUT TimeDisplay sut = new TimeDisplay(); // Inject Stub into SUT sut.setTimeProvider(testStub); // Exercise SUT String result = sut.getCurrentTimeAsHtmlFragment(); // Verify Direct Output String expectedTimeString = "<span class=\"tinyBoldText\">Midnight</span>"; assertEquals("Midnight", expectedTimeString, result); }
This test is hard to understand without seeing the definition of the Hard-Coded Test Double. It is easy to see how this lack of clarity can lead to a Mystery Guest (see Obscure Test) if the definition is not close at hand.
class MidnightTimeProvider implements TimeProvider { public Calendar getTime() { Calendar myTime = new GregorianCalendar(); myTime.set(Calendar.HOUR_OF_DAY, 0); myTime.set(Calendar.MINUTE, 0); return myTime; } }
We can solve the Obscure Test problem by using a Self Shunt (see Hard-Coded Test Double) to make the Hard-Coded Test Double visible within the test:
public class SelfShuntExample extends TestCase implements TimeProvider { public void testDisplayCurrentTime_AtMidnight() throws Exception { // Fixture Setup TimeDisplay sut = new TimeDisplay(); // Mock Setup sut.setTimeProvider(this); // self shunt installation // Exercise SUT String result = sut.getCurrentTimeAsHtmlFragment(); // Verify Direct Output String expectedTimeString = "<span class=\"tinyBoldText\">Midnight</span>"; assertEquals("Midnight", expectedTimeString, result); } public Calendar getTime() { Calendar myTime = new GregorianCalendar(); myTime.set(Calendar.MINUTE, 0); myTime.set(Calendar.HOUR_OF_DAY, 0); return myTime; } }
Unfortunately, we will need to build the Test Double behavior into each Testcase Class that requires it, which results in Test Code Duplication.
Refactoring Notes
Refactoring a test that uses a Hard-Coded Test Double to become a test that uses a third-party Configurable Test Double is relatively straightforward. We simply follow the directions provided with the toolkit to instantiate the Configurable Test Double and configure it with the same values as we used in the Hard-Coded Test Double. We may also have to move some of the logic that was originally hard-coded within the Test Double into the Test Method and pass it in to the Test Double as part of the configuration step.
Converting the actual Hard-Coded Test Double into a Configurable Test Double is a bit more complicated, but not overly so if we need to capture only simple behavior. (For more complex behavior, we’re probably better off examining one of the existing toolkits and porting it to our environment if it is not yet available.) First we need to introduce a way to set the values to be returned or expected. The best choice is to start by modifying the test to see how we want to interact with the Configurable Test Double. After instantiating it during the fixture setup part of the test, we then pass the test-specific values to the Configurable Test Double using the emerging Configuration Interface or Configuration Mode. Once we’ve seen how we want to use the Configurable Test Double, we can use an Introduce Field [JetBrains] refactoring to create the instance variables of the Configurable Test Double to hold each of the previously hard-coded values.
Example: Configuration Interface Using Setters
The following example shows how a test would use a simple hand-built Configuration Interface using Setter Injection:
public void testDisplayCurrentTime_AtMidnight() throws Exception { // Fixture setup // Test Double configuration TimeProviderTestStub tpStub = new TimeProviderTestStub(); tpStub.setHours(0); tpStub.setMinutes(0); // Instantiate SUT TimeDisplay sut = new TimeDisplay(); // Test Double installation sut.setTimeProvider(tpStub); // Exercise SUT String result = sut.getCurrentTimeAsHtmlFragment(); // Verify Outcome String expectedTimeString = "<span class=\"tinyBoldText\">Midnight</span>"; assertEquals("Midnight", expectedTimeString, result); }
The Configurable Test Double is implemented as follows:
class TimeProviderTestStub implements TimeProvider { // Configuration Interface public void setHours(int hours) { // 0 is midnight; 12 is noon myTime.set(Calendar.HOUR_OF_DAY, hours); } public void setMinutes(int minutes) { myTime.set(Calendar.MINUTE, minutes); } // Interface Used by SUT public Calendar getTime() { // @return the last time that was set return myTime; } }
Example: Configuration Interface Using Expression Builder
Now let’s contrast the Configuration Interface we defined in the previous example with the one provided by the JMock framework. JMock generates Mock Objects dynamically and provides a generic fluent interface for configuring the Mock Object in an intent-revealing style. Here’s the same test converted to use JMock:
public void testDisplayCurrentTime_AtMidnight_JM() throws Exception { // Fixture setup TimeDisplay sut = new TimeDisplay(); // Test Double configuration Mock tpStub = mock(TimeProvider.class); Calendar midnight = makeTime(0,0); tpStub.stubs().method("getTime"). withNoArguments(). will(returnValue(midnight)); // Test Double installation sut.setTimeProvider((TimeProvider) tpStub); // Exercise SUT String result = sut.getCurrentTimeAsHtmlFragment(); // Verify Outcome String expectedTimeString = "<span class=\"tinyBoldText\">Midnight</span>"; assertEquals("Midnight", expectedTimeString, result); }
Here we have moved some of the logic to construct the time to be returned into the Testcase Class because there is no way to do it in the generic mocking framework; we’ve used a Test Utility Method to construct the time to be returned. This next example shows a configurable Mock Object complete with multiple expected parameters:
public void testRemoveFlight_JMock() throws Exception { // fixture setup FlightDto expectedFlightDto = createAnonRegFlight(); FlightManagementFacade facade = new FlightManagementFacadeImpl(); // mock configuration Mock mockLog = mock(AuditLog.class); mockLog.expects(once()).method("logMessage") .with(eq(helper.getTodaysDateWithoutTime()), eq(Helper.TEST_USER_NAME), eq(Helper.REMOVE_FLIGHT_ACTION_CODE), eq(expectedFlightDto.getFlightNumber())); // mock installation facade.setAuditLog((AuditLog) mockLog.proxy()); // exercise facade.removeFlight(expectedFlightDto.getFlightNumber()); // verify assertFalse("flight still exists after being removed", facade.flightExists( expectedFlightDto. getFlightNumber())); // verify() method called automatically by JMock }
The Expected Behavior Specification is built by calling expression-building methods such as expects, once, and method to describe how the Configurable Test Double should be used and what it should return. JMock supports the specification of much more sophisticated behavior (such as multiple calls to the same method with different arguments and return values) than does our hand-built Configurable Test Double.
Example: Configuration Mode
In the next example, the test has been converted to use a Mock Object with a Configuration Mode:
public void testRemoveFlight_ModalMock() throws Exception { // fixture setup FlightDto expectedFlightDto = createAnonRegFlight(); // mock configuration (in Configuration Mode) ModalMockAuditLog mockLog = new ModalMockAuditLog(); mockLog.logMessage(Helper.getTodaysDateWithoutTime(), Helper.TEST_USER_NAME, Helper.REMOVE_FLIGHT_ACTION_CODE, expectedFlightDto.getFlightNumber()); mockLog.enterPlaybackMode(); // mock installation FlightManagementFacade facade = new FlightManagementFacadeImpl(); facade.setAuditLog(mockLog); // exercise facade.removeFlight(expectedFlightDto.getFlightNumber()); // verify assertFalse("flight still exists after being removed", facade.flightExists( expectedFlightDto. getFlightNumber())); mockLog.verify(); }
Here the test calls the methods on the Configurable Test Double during the fixture setup phase. If we weren’t aware that this test uses a Configurable Test Double mock, we might find this structure confusing at first glance. The most obvious clue to its intent is the call to the method enterPlaybackMode, which tells the Configurable Test Double to stop saving expected values and to start asserting on them.
The Configurable Test Double used by this test is implemented like this:
private int mode = record; public void enterPlaybackMode() { mode = playback; } public void logMessage( Date date, String user, String action, Object detail) { if (mode == record) { Assert.assertEquals("Only supports 1 expected call", 0, expectedNumberCalls); expectedNumberCalls = 1; expectedDate = date; expectedUser = user; expectedCode = action; expectedDetail = detail; } else { Assert.assertEquals("Date", expectedDate, date); Assert.assertEquals("User", expectedUser, user); Assert.assertEquals("Action", expectedCode, action); Assert.assertEquals("Detail", expectedDetail, detail); } }
The if statement checks whether we are in record or playback mode. Because this simple hand-built Configurable Test Double allows only a single value to be stored, a Guard Assertion fails the test if it tries to record more than one call to this method. The rest of the then clause saves the parameters into variables that it uses as the expected values of the Equality Assertions (see Assertion Method) in the else clause.