Test Stub
How can we verify logic independently when it depends on indirect inputs from other software components?
We replace a real object with a test-specific object that feeds the desired indirect inputs into the system under test.
In many circumstances, the environment or context in which the SUT operates very much influences the behavior of the SUT. To get adequate control over the indirect inputs of the SUT, we may have to replace some of the context with something we can control—namely, a Test Stub.
How It Works
First, we define a test-specific implementation of an interface on which the SUT depends. This implementation is configured to respond to calls from the SUT with the values (or exceptions) that will exercise the Untested Code (see Production Bugs) within the SUT. Before exercising the SUT, we install the Test Stub so that the SUT uses it instead of the real implementation. When called by the SUT during test execution, the Test Stub returns the previously defined values. The test can then verify the expected outcome in the normal way.
When to Use It
A key indication for using a Test Stub is having Untested Code caused by our inability to control the indirect inputs of the SUT. We can use a Test Stub as a control point that allows us to control the behavior of the SUT with various indirect inputs and we have no need to verify the indirect outputs. We can also use a Test Stub to inject values that allow us to get past a particular point in the software where the SUT calls software that is unavailable in our test environment.
If we do need an observation point that allows us to verify the indirect outputs of the SUT, we should consider using a Mock Object or a Test Spy. Of course, we must have a way of installing a Test Double into the SUT to be able to use any form of Test Double.
Variation: Responder
A Test Stub that is used to inject valid indirect inputs into the SUT so that it can go about its business is called a Responder. Responders are commonly used in “happy path” testing when the real component is uncontrollable, is not yet available, or is unusable in the development environment. The tests will invariably be Simple Success Tests (see Test Method).
Variation: Saboteur
A Test Stub that is used to inject invalid indirect inputs into the SUT is often called a Saboteur because its purpose is to derail whatever the SUT is trying to do so that we can see how the SUT copes under these circumstances. The “derailment” might be caused by returning unexpected values or objects, or it might result from raising an exception or causing a runtime error. Each test may be either a Simple Success Test or an Expected Exception Test (see Test Method), depending on how the SUT is expected to behave in response to the indirect input.
Variation: Temporary Test Stub
A Temporary Test Stub stands in for a DOC that is not yet available. This kind of Test Stub typically consists of an empty shell of a real class with hard-coded return statements. As soon as the real DOC is available, it replaces the Temporary Test Stub. Test-driven development often requires us to create Temporary Test Stubs as we write code from the outside in; these shells evolve into the real classes as we add code to them. In need-driven development, we tend to use Mock Objects because we want to verify that the SUT calls the right methods on the Temporary Test Stub; in addition, we typically continue using the Mock Object even after the real DOC becomes available.
Variation: Procedural Test Stub
A Procedural Test Stub is a Test Stub written in a procedural programming language. It is particularly challenging to create in procedural programming languages that do not support procedure variables (also known as function pointers). In most cases, we must put if testing then hooks into the production code (a form of Test Logic in Production).
Variation: Entity Chain Snipping
Entity Chain Snipping (see Test Stub) is a special case of a Responder that is used to replace a complex network of objects with a single Test Stub that pretends to be the network of objects. Its inclusion can make fixture setup go much more quickly (especially when the objects would normally have to be persisted into a database) and can make the tests much easier to understand.
Implementation Notes
We must be careful when using Test Stubs because we are testing the SUT in a different configuration from the one that will be used in production. We really should have at least one test that verifies the SUT works without a Test Stub. A common mistake made by test automaters who are new to stubs is to replace a part of the SUT that they are trying to test. For this reason, it is important to be really clear about what is playing the role of SUT and what is playing the role of test fixture. Also, note that excessive use of Test Stubs can result in Overspecified Software (see Fragile Test).
Test Stubs may be built in several different ways depending on our specific needs and the tools we have on hand.
Variation: Hard-Coded Test Stub
A Hard-Coded Test Stub has its responses hard-coded within its program logic. These Test Stubs tend to be purpose-built for a single test or a very small number of tests. See Hard-Coded Test Double for more information.
Variation: Configurable Test Stub
When we want to avoid building a different Hard-Coded Test Stub for each test, we can use a Configurable Test Stub (see Configurable Test Double). A test configures the Configurable Test Stub as part of its fixture setup phase. Many members of the xUnit family offer tools with which to generate Configurable Test Doubles, including Configurable Test Stubs.
Motivating Example
The following test verifies the basic functionality of a component that formats an HTML string containing the current time. Unfortunately, it depends on the real system clock so it rarely ever passes!
public void testDisplayCurrentTime_AtMidnight() { // fixture setup TimeDisplay sut = new TimeDisplay(); // exercise SUT String result = sut.getCurrentTimeAsHtmlFragment(); // verify direct output String expectedTimeString = "<span class=\"tinyBoldText\">Midnight</span>"; assertEquals( expectedTimeString, result); }
We could try to address this problem by making the test calculate the expected results based on the current system time as follows:
public void testDisplayCurrentTime_whenever() { // fixture setup TimeDisplay sut = new TimeDisplay(); // exercise SUT String result = sut.getCurrentTimeAsHtmlFragment(); // verify outcome Calendar time = new DefaultTimeProvider().getTime(); StringBuffer expectedTime = new StringBuffer(); expectedTime.append("<span class=\"tinyBoldText\">"); if ((time.get(Calendar.HOUR_OF_DAY) == 0) && (time.get(Calendar.MINUTE) <= 1)) { expectedTime.append( "Midnight"); } else if ((time.get(Calendar.HOUR_OF_DAY) == 12) && (time.get(Calendar.MINUTE) == 0)) { // noon expectedTime.append("N3oon"); } else { SimpleDateFormat fr = new SimpleDateFormat("h:mm a"); expectedTime.append(fr.format(time.getTime())); } expectedTime.append("</span>"); assertEquals( expectedTime, result); }
This Flexible Test (see Conditional Test Logic on page 200) introduces two problems. First, some test conditions are never exercised. (Do you want to come in to work to run the tests at midnight to prove the software works at midnight?) Second, the test needs to duplicate much of the logic in the SUT to calculate the expected results. How do we prove the logic is actually correct?
Refactoring Notes
We can achieve proper verification of the indirect inputs by getting control of the time. To do so, we use the Replace Dependency with Test Double refactoring to replace the real system clock (represented here by TimeProvider) with a Virtual Clock [VCTP]. We then implement it as a Test Stub that is configured by the test with the time we want to use as the indirect input to the SUT.
Example: Responder (as Hand-Coded Test Stub)
The following test verifies one of the happy path test conditions using a Responder to get control over the indirect inputs of the SUT. Based on the time injected into the SUT, the expected result can be hard-coded safely.
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); }
This test makes use of the following hand-coded configurable Test Stub implementation:
private Calendar myTime = new GregorianCalendar(); /** * The complete constructor for the TimeProviderTestStub * @param hours specifies the hours using a 24-hour clock * (e.g., 10 = 10 AM, 12 = noon, 22 = 10 PM, 0 = midnight) * @param minutes specifies the minutes after the hour * (e.g., 0 = exactly on the hour, 1 = 1 min after the hour) */ public TimeProviderTestStub(int hours, int minutes) { setTime(hours, minutes); } public void setTime(int hours, int minutes) { setHours(hours); setMinutes(minutes); } // 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: Responder (Dynamically Generated)
Here’s the same test coded using the JMock Configurable Test Double framework:
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); }
There is no Test Stub implementation to examine for this test because the JMock framework implements the Test Stub using reflection. Thus we had to write a Test Utility Method called makeTime that contains the logic to construct the Calendar object to be returned. In the hand-coded Test Stub, this logic appeared inside the getTime method.
Example: Saboteur (as Anonymous Inner Class)
The following test uses a Saboteur to inject invalid indirect inputs into the SUT so we can see how the SUT copes under these circumstances.
public void testDisplayCurrentTime_exception() throws Exception { // Fixture setup // Define and instantiate Test Stub TimeProvider testStub = new TimeProvider() { // Anonymous inner Test Stub public Calendar getTime() throws TimeProviderEx { throw new TimeProviderEx("Sample"); } }; // Instantiate SUT TimeDisplay sut = new TimeDisplay(); sut.setTimeProvider(testStub); // Exercise SUT String result = sut.getCurrentTimeAsHtmlFragment(); // Verify direct output String expectedTimeString = "<span class=\"error\">Invalid Time</span>"; assertEquals("Exception", expectedTimeString, result); }
In this case, we used an Inner Test Double (see Hard-Coded Test Double) to throw an exception that we expect the SUT to handle gracefully. One interesting thing about this test is that it uses the Simple Success Test method template rather than the Expected Exception Test template, even though we are injecting an exception as the indirect input. The rationale behind this choice is that we are expecting the SUT to catch the exception and change the string formatting; we are not expecting the SUT to throw an exception.
Example: Entity Chain Snipping
In this example, we are testing the Invoice but require a Customer to instantiate the Invoice. The Customer requires an Address, which in turn requires a City. Thus we find ourselves creating numerous additional objects just to set up the fixture. Suppose the behavior of the invoice depends on some attribute of the Customer that is calculated from the Address by calling the method get_zone on the Customer.
public void testInvoice_addLineItem_noECS() { final int QUANTITY = 1; Product product = new Product(getUniqueNumberAsString(), getUniqueNumber()); State state = new State("West Dakota", "WD"); City city = new City("Centreville", state); Address address = new Address("123 Blake St.", city, "12345"); Customer customer= new Customer(getUniqueNumberAsString(), getUniqueNumberAsString(), address); Invoice inv = new Invoice(customer); // Exercise inv.addItemQuantity(product, QUANTITY); // Verify List lineItems = inv.getLineItems(); assertEquals("number of items", lineItems.size(), 1); LineItem actual = (LineItem)lineItems.get(0); LineItem expItem = new LineItem(inv, product, QUANTITY); assertLineItemsEqual("",expItem, actual); }
In this test, we want to verify only the behavior of the invoice logic that depends on this zone attribute—not the way this attribute is calculated from the Customer’s address. (There are separate Customer unit tests to verify the zone is calculated correctly.) All of the setup of the address, city, and other information merely distracts the reader.
Here’s the same test using a Test Stub instead of the Customer. Note how much simpler the fixture setup has become as a result of Entity Chain Snipping!
public void testInvoice_addLineItem_ECS() { final int QUANTITY = 1; Product product = new Product(getUniqueNumberAsString(), getUniqueNumber()); Mock customerStub = mock(ICustomer.class); customerStub.stubs().method("getZone").will(returnValue(ZONE_3)); Invoice inv = new Invoice((ICustomer)customerStub.proxy()); // Exercise inv.addItemQuantity(product, QUANTITY); // Verify List lineItems = inv.getLineItems(); assertEquals("number of items", lineItems.size(), 1); LineItem actual = (LineItem)lineItems.get(0); LineItem expItem = new LineItem(inv, product, QUANTITY); assertLineItemsEqual("", expItem, actual); }
We have used JMock to stub out the Customer with a customerStub that returns ZONE_3 when getZone is called. This is all we need to verify the Invoice behavior, and we have managed to get rid of all that distracting extra object construction. It is also much clearer from reading this test that invoicing behavior depends only on the value returned by get_zone and not any other attributes of the Customer or Address.
Further Reading
Almost every book on automated testing using xUnit has something to say about Test Stubs, so I won’t list those resources here. As you are reading other books, however, keep in mind that the term Test Stub is often used to refer to a Mock Object. Mocks, Fakes, Stubs, and Dummies (in Appendix B) contains a more thorough comparison of the terminology used in various books and articles.
Sven Gorts describes a number of different ways we can use a Test Stub [UTwHCM]. I have adopted many of his names and adapted a few to better fit into this pattern language. Paolo Perrotta wrote a pattern describing a common example of a Responder called Virtual Clock. He uses a Test Stub as a Decorator [GOF] for the real system clock that allows the time to be “frozen” or resumed. Of course, we could use a Hard-Coded Test Stub or a Configurable Test Stub just as easily for most tests.