- Test Double
- Test Stub
- Test Spy
- Mock Object
- Fake Object
- Configurable Test Double
- Hard-Coded Test Double
- Test-Specific Subclass
Test-Specific Subclass
How can we make code testable when we need to access private state of the SUT?
We add methods that expose the state or behavior needed by the test to a subclass of the SUT.
If the SUT was not designed specifically to be testable, we may find that the test cannot gain access to a state that it must initialize or verify at some point in the test.
A Test-Specific Subclass is a simple yet very powerful way to open up the SUT for testing purposes without modifying the code of the SUT itself.
How It Works
We define a subclass of the SUT and add methods that modify the behavior of the SUT just enough to make it testable by implementing control points and observation points. This effort typically involves exposing instance variables using setters and getters or perhaps adding a method to put the SUT into a specific state without moving through its entire life cycle.
Because the Test-Specific Subclass would be packaged together with the tests that use it, the use of a Test-Specific Subclass does not change how the SUT is seen by the rest of the application.
When to Use It
We should use a Test-Specific Subclass whenever we need to modify the SUT to improve its testability but doing so directly would result in Test Logic in Production. Although we can use a Test-Specific Subclass for a number of purposes, all of those scenarios share a common goal: They improve testability by letting us get at the insides of the SUT more easily. A Test-Specific Subclass can be a double-edged sword, however. By breaking encapsulation, it allows us to tie our tests even more closely to the implementation, which can in turn result in Fragile Tests.
Variation: State-Exposing Subclass
If we are doing State Verification , we can subclass the SUT (or some component of it) so that we can see the internal state of the SUT for use in Assertion Methods. Usually, this effort involves adding accessor methods for private instance variables. We may also allow the test to set the state as a way to avoid Obscure Tests caused by Obscure Setup (see Obscure Test) logic.
Variation: Behavior-Exposing Subclass
If we want to test the individual steps of a complex algorithm individually, we can subclass the SUT to expose the private methods that implement the Self-Calls [WWW]. Because most languages do not allow for relaxing the visibility of a method, we often have to use a different name in the Test-Specific Subclass and make a call to the superclass’s method.
Variation: Behavior-Modifying Subclass
If the SUT contains some behavior that we do not want to occur when testing, we can override whatever method implements the behavior with an empty method body. This technique works best when the SUT uses Self-Calls (or a Template Method [GOF]) to delegate the steps of an algorithm to methods on itself or subclasses.
Variation: Test Double Subclass
To ensure that a Test Double is type-compatible with a DOC we wish to replace, we can make the Test Double a subclass of that component. This may be the only way we can build a Test Double that the compiler will accept when variables are statically typed using concrete classes.[5] (We should not have to take this step with dynamically typed languages such as Ruby, Python, Perl, and JavaScript.) We then override any methods whose behavior we want to change and add any methods we require to transform the Test Double into a Configurable Test Double if we so desire.
Unlike the Behavior-Modifying Subclass, the Test Double Subclass does not just “tweak” the behavior of the SUT (or a part thereof) but replaces it entirely with canned behavior.
Variation: Substituted Singleton
The Substituted Singleton is a special case of Test Double Subclass. We use it when we want to replace a DOC with a Test Double and the SUT does not support Dependency Injection or Dependency Lookup.
Implementation Notes
The use of a Test-Specific Subclass brings some challenges:
- Feature granularity: ensuring that any behavior we want to override or expose is in its own single-purpose method. It is enabled through copious use of small methods and Self-Calls.
- Feature visibility: ensuring that subclasses can access attributes and behavior of the SUT class. It is primarily an issue in statically typed languages such as Java, C#, and C++; dynamically typed languages typically do not enforce visibility.
As with Test Doubles, we must be careful to ensure that we do not replace any of the behavior we are actually trying to test.
In languages that support class extensions without the need for subclassing (e.g., Smalltalk, Ruby, JavaScript, and other dynamic languages), a Test-Specific Subclass can be implemented as a class extension in the test package. We need to be aware, however, whether the extensions will make it into production; doing so would introduce Test Logic in Production.
Visibility of Features
In languages that enforce scope (visibility) of variables and methods, we may need to change the visibility of the variables to allow subclasses to access them. While such a change affects the actual SUT code, it would typically be considered much less intrusive or misleading than changing the visibility to public (thereby allowing any code in the application to access the variables) or adding the test-specific methods directly to the SUT.
For example, in Java, we might change the visibility of instance variables from private to protected to allow the Test-Specific Subclass to access them. Similarly, we might change the visibility of methods to allow the Test-Specific Subclass to call them.
Granularity of Features
Long methods are difficult to test because they often bring too many dependencies into play. By comparison, short methods tend to be much simpler to test because they do only one thing. Self-Call offers an easy way to reduce the size of methods. We delegate parts of an algorithm to other methods implemented on the same class. This strategy allows us to test these methods independently. We can also confirm that the calling method calls these methods in the right sequence by overriding them in a Test Double Subclass (see Test-Specific Subclass).
Self-Call is a part of good object-oriented code design in that it keeps methods small and focused on implementing a single responsibility of the SUT. We can use this pattern whenever we are doing test-driven development and have control over the design of the SUT. We may find that we need to introduce Self-Call when we encounter long methods where some parts of the algorithm depend on things we do not want to exercise (e.g., database calls). This likelihood is especially high, for example, when the SUT is built using a Transaction Script [PEAA] architecture. Self-Call can be retrofitted easily using the Extract Method [Fowler] refactoring supported by most modern IDEs.
Motivating Example
The test in the following example is nondeterministic because it depends on the time. Our SUT is an object that formats the time for display as part of a Web page. It gets the time by asking a Singleton called TimeProvider to retrieve the time from a calendar object that it gets from the container.
public void testDisplayCurrentTime_AtMidnight() throws Exception { // Set up SUT TimeDisplay theTimeDisplay = new TimeDisplay(); // Exercise SUT String actualTimeString = theTimeDisplay.getCurrentTimeAsHtmlFragment(); // Verify outcome String expectedTimeString = "<span class=\"tinyBoldText\">Midnight</span>"; assertEquals( "Midnight", expectedTimeString, actualTimeString); } public void testDisplayCurrentTime_AtOneMinuteAfterMidnight() throws Exception { // Set up SUT TimeDisplay actualTimeDisplay = new TimeDisplay(); // Exercise SUT String actualTimeString = actualTimeDisplay.getCurrentTimeAsHtmlFragment(); // Verify outcome String expectedTimeString = "<span class=\"tinyBoldText\">12:01 AM</span>"; assertEquals( "12:01 AM", expectedTimeString, actualTimeString); }
These tests rarely pass, and they never pass in the same test run! The code within the SUT looks like this:
public String getCurrentTimeAsHtmlFragment() { Calendar timeProvider; try { timeProvider = getTime(); } catch (Exception e) { return e.getMessage(); } // etc. } protected Calendar getTime() { return TimeProvider.getInstance().getTime(); }
The code for the Singleton follows:
public class TimeProvider { protected static TimeProvider soleInstance = null; protected TimeProvider() {}; public static TimeProvider getInstance() { if (soleInstance==null) soleInstance = new TimeProvider(); return soleInstance; } public Calendar getTime() { return Calendar.getInstance(); } }
Refactoring Notes
The precise nature of the refactoring employed to introduce a Test-Specific Subclass depends on why we are using one. When we are using a Test-Specific Subclass to expose “private parts” of the SUT or override undesirable parts of its behavior, we merely define the Test-Specific Subclass as a subclass of the SUT and create an instance of the Test-Specific Subclass to exercise in the setup fixture phase of our Four-Phase Test.
When we are using the Test-Specific Subclass to replace a DOC of the SUT, however, we need to use a Replace Dependency with Test Double refactoring to tell the SUT to use our Test-Specific Subclass instead of the real DOC.
In either case, we either override existing methods or add new methods to the Test-Specific Subclass using our language-specific capabilities (e.g., subclassing or mixins) as required by our tests.
Example: Behavior-Modifying Subclass (Test Stub)
Because the SUT uses a Self-Call to the getTime method to ask the TimeProvider for the time, we have an opportunity to use a Subclassed Test Double to control the time.[6] Based on this idea we can take a stab at writing our tests as follows (I have shown only one test here):
public void testDisplayCurrentTime_AtMidnight() { // Fixture setup TimeDisplayTestStubSubclass tss = new TimeDisplayTestStubSubclass(); TimeDisplay sut = tss; // Test Double configuration tss.setHours(0); tss.setMinutes(0); // Exercise SUT String result = sut.getCurrentTimeAsHtmlFragment(); // Verify outcome String expectedTimeString = "<span class=\"tinyBoldText\">Midnight</span>"; assertEquals( expectedTimeString, result ); }
Note that we have used the Test-Specific Subclass class for the variable that receives the instance of the SUT; this approach ensures that the methods of the Configuration Interface (see Configurable Test Double) defined on the Test-Specific Subclass are visible to the test.[7] For documentation purposes, we have then assigned the Test-Specific Subclass to the variable sut; this is a safe cast because the Test-Specific Subclass class is a subclass of the SUT class. This technique also helps us avoid the Mystery Guest (see Obscure Test) problem caused by hard-coding an important indirect input of our SUT inside the Test Stub.
Now that we have seen how it will be used, it is a simple matter to implement the Test-Specific Subclass:
public class TimeDisplayTestStubSubclass extends TimeDisplay { private int hours; private int minutes; // Overridden method protected Calendar getTime() { Calendar myTime = new GregorianCalendar(); myTime.set(Calendar.HOUR_OF_DAY, this.hours); myTime.set(Calendar.MINUTE, this.minutes); return myTime; } /* * Configuration Interface */ public void setHours(int hours) { this.hours = hours; } public void setMinutes(int minutes) { this.minutes = minutes; } }
There’s no rocket science here—we just had to implement the methods used by the test.
Example: Behavior-Modifying Subclass (Substituted Singleton)
Suppose our getTime method was declared to be private[8] or static, final or sealed, and so on.[9] Such a declaration would prevent us from overriding the method’s behavior in our Test-Specific Subclass. What could we do to address our Nondeterministic Tests (see Erratic Test)?
Because the design uses a Singleton [GOF] to provide the time, a simple solution is to replace the Singleton during test execution with a Test Double Subclass. We can do so as long as it is possible for a subclass to access its soleInstance variable. We use the Introduce Local Extension [Fowler] refactoring (specifically, the subclass variant of it) to create the Test-Specific Subclass. Writing the tests first helps us understand the interface we want to implement.
public void testDisplayCurrentTime_AtMidnight() { TimeDisplay sut = new TimeDisplay(); // Install test Singleton TimeProviderTestSingleton timeProvideSingleton = TimeProviderTestSingleton.overrideSoleInstance(); timeProvideSingleton.setTime(0,0); // Exercise SUT String actualTimeString = sut.getCurrentTimeAsHtmlFragment(); // Verify outcome String expectedTimeString = "<span class=\"tinyBoldText\">Midnight</span>"; assertEquals( expectedTimeString, actualTimeString ); }
Now that we have a test that uses the Substituted Singleton, we can proceed to implement it by subclassing the Singleton and defining the methods the tests will use.
public class TimeProviderTestSingleton extends TimeProvider { private Calendar myTime = new GregorianCalendar(); private TimeProviderTestSingleton() {}; // Installation Interface static TimeProviderTestSingleton overrideSoleInstance() { // We could save the real instance first, but we won't! soleInstance = new TimeProviderTestSingleton(); return (TimeProviderTestSingleton) soleInstance; } // Configuration Interface used by the test public void setTime(int hours, int minutes) { myTime.set(Calendar.HOUR_OF_DAY, hours); myTime.set(Calendar.MINUTE, minutes); } // Usage Interface used by the client public Calendar getTime() { return myTime; } }
Here the Test Double is a subclass of the real component and has overridden the instance method called by the clients of the Singleton.
Example: Behavior-Exposing Subclass
Suppose we wanted to test the getTime method directly. Because getTime is protected and our test is in a different package from the TimeDisplay class, our test cannot call this method. We could try making our test a subclass of TimeDisplay or we could put it into the same package as TimeDisplay. Unfortunately, both of these solutions come with baggage and may not always be possible.
A more general solution is to expose the behavior using a Behavior-Exposing Subclass. We can do so by defining a Test-Specific Subclass and adding a public method that calls this method.
public class TimeDisplayBehaviorExposingTss extends TimeDisplay { public Calendar callGetTime() { return super.getTime(); } }
We can now write the test using the Behavior-Exposing Subclass as follows:
public void testGetTime_default() { // create SUT TimeDisplayBehaviorExposingTss tsSut = new TimeDisplayBehaviorExposingTss(); // exercise SUT // want to do // Calendar time = sut.getTime(); // have to do Calendar time = tsSut.callGetTime(); // verify outcome assertEquals( defaultTime, time ); }
Example: Defining Test-Specific Equality (Behavior-Modifying Subclass)
Here is an example of a very simple test that fails because the object we pass to assertEquals does not implement test-specific equality. That is, the default equals method returns false even though our test considers the two objects to be equals.
protected void setUp() throws Exception { oneOutboundFlight = findOneOutboundFlightDto(); } public void testGetFlights_OneFlight() throws Exception { // Exercise System List flights = facade.getFlightsByOriginAirport( oneOutboundFlight.getOriginAirportId()); // Verify Outcome assertEquals("Flights at origin - number of flights: ", 1, flights.size()); FlightDto actualFlightDto = (FlightDto)flights.get(0); assertEquals("Flight DTOs at origin", oneOutboundFlight, actualFlightDto); }
One option is to write a Custom Assertion. Another option is to use a Test-Specific Subclass to add a more appropriate definition of equality for our test purposes alone. We can change our fixture setup code slightly to create the Test-Specific Subclass as our Expected Object (see State Verification).
private FlightDtoTss oneOutboundFlight; private FlightDtoTss findOneOutboundFlightDto() { FlightDto realDto = helper.findOneOutboundFlightDto(); return new FlightDtoTss(realDto) ; }
Finally, we implement the Test-Specific Subclass by copying and comparing only those fields that we want to use for our test-specific equality.
public class FlightDtoTss extends FlightDto { public FlightDtoTss(FlightDto realDto) { this.destAirportId = realDto.getDestinationAirportId(); this.equipmentType = realDto.getEquipmentType(); this.flightNumber = realDto.getFlightNumber(); this.originAirportId = realDto.getOriginAirportId(); } public boolean equals(Object obj) { FlightDto otherDto = (FlightDto) obj; if (otherDto == null) return false; if (otherDto.getDestAirportId()!= this.destAirportId) return false; if (otherDto.getOriginAirportId()!= this.originAirportId) return false; if (otherDto.getFlightNumber()!= this.flightNumber) return false; if (otherDto.getEquipmentType() != this.equipmentType ) return false; return true; } }
In this case we copied the fields from the real DTO into our Test-Specific Subclass, but we could just as easily have used the Test-Specific Subclass as a wrapper for the real DTO. There are other ways we could have created the Test-Specific Subclass; the only real limit is our imagination.
This example also assumes that we have a reasonable toString implementation on our base class that prints out the values of the fields being compared. It is needed because assertEquals will use that implementation when the equals method returns false. Otherwise, we will have no idea of why the objects are considered unequal.
Example: State-Exposing Subclass
Suppose we have the following test, which requires a Flight to be in a particular state:
protected void setUp() throws Exception { super.setUp(); scheduledFlight = createScheduledFlight(); } Flight createScheduledFlight() throws InvalidRequestException{ Flight newFlight = new Flight(); newFlight.schedule(); return newFlight; } public void testDeschedule_shouldEndUpInUnscheduleState() throws Exception { scheduledFlight.deschedule(); assertTrue("isUnsched", scheduledFlight.isUnscheduled()); }
Setting up the fixture for this test requires us to call the method schedule on the flight:
public class Flight{ protected FlightState currentState = new UnscheduledState(); /** * Transitions the Flight from the <code>unscheduled</code> * state to the <code>scheduled</code> state. * @throws InvalidRequestException when an invalid state * transition is requested */ public void schedule() throws InvalidRequestException{ currentState.schedule(); } }
The Flight class uses the State [GOF] pattern and delegates handling of the schedule method to whatever State object is currently referenced by currentState. This test will fail during fixture setup if schedule does not work yet on the default content of currentState. We can avoid this problem by using a State-Exposing Subclass that provides a method to move directly into the state, thereby making this an Independent Test.
public class FlightTss extends Flight { public void becomeScheduled() { currentState = new ScheduledState(); } }
By introducing a new method becomeScheduled on the Test-Specific Subclass, we ensure that we will not accidentally override any existing behavior of the SUT. Now all we have to do is instantiate the Test-Specific Subclass in our test instead of the base class by modifying our Creation Method.
Flight createScheduledFlight() throws InvalidRequestException{ FlightTss newFlight = new FlightTss(); newFlight.becomeScheduled(); return newFlight; }
Note how we still declare that we are returning an instance of the Flight class when we are, in fact, returning an instance of the Test-Specific Subclass that has the additional method.