Home > Articles > Software Development & Management

This chapter is from the book 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.

InformIT Promotional Mailings & Special Offers

I would like to receive exclusive offers and hear about products from InformIT and its family of brands. I can unsubscribe at any time.

Overview


Pearson Education, Inc., 221 River Street, Hoboken, New Jersey 07030, (Pearson) presents this site to provide information about products and services that can be purchased through this site.

This privacy notice provides an overview of our commitment to privacy and describes how we collect, protect, use and share personal information collected through this site. Please note that other Pearson websites and online products and services have their own separate privacy policies.

Collection and Use of Information


To conduct business and deliver products and services, Pearson collects and uses personal information in several ways in connection with this site, including:

Questions and Inquiries

For inquiries and questions, we collect the inquiry or question, together with name, contact details (email address, phone number and mailing address) and any other additional information voluntarily submitted to us through a Contact Us form or an email. We use this information to address the inquiry and respond to the question.

Online Store

For orders and purchases placed through our online store on this site, we collect order details, name, institution name and address (if applicable), email address, phone number, shipping and billing addresses, credit/debit card information, shipping options and any instructions. We use this information to complete transactions, fulfill orders, communicate with individuals placing orders or visiting the online store, and for related purposes.

Surveys

Pearson may offer opportunities to provide feedback or participate in surveys, including surveys evaluating Pearson products, services or sites. Participation is voluntary. Pearson collects information requested in the survey questions and uses the information to evaluate, support, maintain and improve products, services or sites, develop new products and services, conduct educational research and for other purposes specified in the survey.

Contests and Drawings

Occasionally, we may sponsor a contest or drawing. Participation is optional. Pearson collects name, contact information and other information specified on the entry form for the contest or drawing to conduct the contest or drawing. Pearson may collect additional personal information from the winners of a contest or drawing in order to award the prize and for tax reporting purposes, as required by law.

Newsletters

If you have elected to receive email newsletters or promotional mailings and special offers but want to unsubscribe, simply email information@informit.com.

Service Announcements

On rare occasions it is necessary to send out a strictly service related announcement. For instance, if our service is temporarily suspended for maintenance we might send users an email. Generally, users may not opt-out of these communications, though they can deactivate their account information. However, these communications are not promotional in nature.

Customer Service

We communicate with users on a regular basis to provide requested services and in regard to issues relating to their account we reply via email or phone in accordance with the users' wishes when a user submits their information through our Contact Us form.

Other Collection and Use of Information


Application and System Logs

Pearson automatically collects log data to help ensure the delivery, availability and security of this site. Log data may include technical information about how a user or visitor connected to this site, such as browser type, type of computer/device, operating system, internet service provider and IP address. We use this information for support purposes and to monitor the health of the site, identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents and appropriately scale computing resources.

Web Analytics

Pearson may use third party web trend analytical services, including Google Analytics, to collect visitor information, such as IP addresses, browser types, referring pages, pages visited and time spent on a particular site. While these analytical services collect and report information on an anonymous basis, they may use cookies to gather web trend information. The information gathered may enable Pearson (but not the third party web trend services) to link information with application and system log data. Pearson uses this information for system administration and to identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents, appropriately scale computing resources and otherwise support and deliver this site and its services.

Cookies and Related Technologies

This site uses cookies and similar technologies to personalize content, measure traffic patterns, control security, track use and access of information on this site, and provide interest-based messages and advertising. Users can manage and block the use of cookies through their browser. Disabling or blocking certain cookies may limit the functionality of this site.

Do Not Track

This site currently does not respond to Do Not Track signals.

Security


Pearson uses appropriate physical, administrative and technical security measures to protect personal information from unauthorized access, use and disclosure.

Children


This site is not directed to children under the age of 13.

Marketing


Pearson may send or direct marketing communications to users, provided that

  • Pearson will not use personal information collected or processed as a K-12 school service provider for the purpose of directed or targeted advertising.
  • Such marketing is consistent with applicable law and Pearson's legal obligations.
  • Pearson will not knowingly direct or send marketing communications to an individual who has expressed a preference not to receive marketing.
  • Where required by applicable law, express or implied consent to marketing exists and has not been withdrawn.

Pearson may provide personal information to a third party service provider on a restricted basis to provide marketing solely on behalf of Pearson or an affiliate or customer for whom Pearson is a service provider. Marketing preferences may be changed at any time.

Correcting/Updating Personal Information


If a user's personally identifiable information changes (such as your postal address or email address), we provide a way to correct or update that user's personal data provided to us. This can be done on the Account page. If a user no longer desires our service and desires to delete his or her account, please contact us at customer-service@informit.com and we will process the deletion of a user's account.

Choice/Opt-out


Users can always make an informed choice as to whether they should proceed with certain services offered by InformIT. If you choose to remove yourself from our mailing list(s) simply visit the following page and uncheck any communication you no longer want to receive: www.informit.com/u.aspx.

Sale of Personal Information


Pearson does not rent or sell personal information in exchange for any payment of money.

While Pearson does not sell personal information, as defined in Nevada law, Nevada residents may email a request for no sale of their personal information to NevadaDesignatedRequest@pearson.com.

Supplemental Privacy Statement for California Residents


California residents should read our Supplemental privacy statement for California residents in conjunction with this Privacy Notice. The Supplemental privacy statement for California residents explains Pearson's commitment to comply with California law and applies to personal information of California residents collected in connection with this site and the Services.

Sharing and Disclosure


Pearson may disclose personal information, as follows:

  • As required by law.
  • With the consent of the individual (or their parent, if the individual is a minor)
  • In response to a subpoena, court order or legal process, to the extent permitted or required by law
  • To protect the security and safety of individuals, data, assets and systems, consistent with applicable law
  • In connection the sale, joint venture or other transfer of some or all of its company or assets, subject to the provisions of this Privacy Notice
  • To investigate or address actual or suspected fraud or other illegal activities
  • To exercise its legal rights, including enforcement of the Terms of Use for this site or another contract
  • To affiliated Pearson companies and other companies and organizations who perform work for Pearson and are obligated to protect the privacy of personal information consistent with this Privacy Notice
  • To a school, organization, company or government agency, where Pearson collects or processes the personal information in a school setting or on behalf of such organization, company or government agency.

Links


This web site contains links to other sites. Please be aware that we are not responsible for the privacy practices of such other sites. We encourage our users to be aware when they leave our site and to read the privacy statements of each and every web site that collects Personal Information. This privacy statement applies solely to information collected by this web site.

Requests and Contact


Please contact us about this Privacy Notice or if you have any requests or questions relating to the privacy of your personal information.

Changes to this Privacy Notice


We may revise this Privacy Notice through an updated posting. We will identify the effective date of the revision in the posting. Often, updates are made to provide greater clarity or to comply with changes in regulatory requirements. If the updates involve material changes to the collection, protection, use or disclosure of Personal Information, Pearson will provide notice of the change through a conspicuous notice on this site or other appropriate way. Continued use of the site after the effective date of a posted revision evidences acceptance. Please contact us if you have questions or concerns about the Privacy Notice or any objection to any revisions.

Last Update: November 17, 2020