Search code examples
unit-testinglanguage-agnostictdd

unit-test the template method design pattern


Suppose the following template method implementation:

public abstract class Abs
{
    public void DoYourThing()
    {
        log.Info("Starting");
        try
        {
            DoYourThingInternal();
            log.Info("All done");
        }
        catch (MyException ex)
        {
            log.Error("Something went wrong here!");
            throw;
        }
    }

    protected abstract void DoYourThingInternal();    
}

Now, there are plenty of info around on how to test the Abs class, and make sure that DoYourThingInternal is called.
However, suppose I want to test my Conc class:

 public class Conc : Abs
 {
     protected override void DoYourThingInternal()
     {
         // Lots of awesome stuff here!
     }
 }

I wouldn't want to do conc.DoYourThing(), since this will invoke the parent method, which was already tested separately.

I would like to test only the overriding method.

Any ideas?


Solution

  • I wouldn't consider DoYourThingInternal() to be separate from DoYourThing() (as in, two separate modules of code that can be tested in isolation) since you won't be able to instantiate your abstract class alone anyways and the 2 methods will always be run together. Besides, DoYourThingInternal() has access to all protected members of your class and could modify them, with potential side effects on DoYourThing(). So I think it would be dangerous to test DoYourThing() in complete isolation from a concrete implementation of DoYourThingInternal().

    However, that doesn't mean you can't have separate tests for DoYourThing()'s expected behavior, which has to remain the same across all implementations of Abs, and DoYourThingInternal()'s expected behavior.

    You could use an (abstract) base test class where you define a test for the general behavior expected from DoYourThing(). Then create as many test subclasses as there are implementations of Abs, with unit tests for the specifics of each implementation.

    The test from the base test class will be inherited, and when you run any subclass's tests, the inherited test for DoYourThing() will also run :

    public abstract class AbsBaseTest
    {
      public abstract Abs GetAbs();
    
      [Test]
      public void TestSharedBehavior() 
      {
        getAbs().DoYourThing();
    
        // Test shared behavior here...
      }
    }
    
    [TestFixture]
    public class AbsImplTest : AbsBaseTest
    {
      public override Abs GetAbs()
      {
        return new AbsImpl();
      }
    
      [Test]
      public void TestParticularBehavior()
      {
        getAbs().DoYourThing();
    
        // Test specific behavior here
      }
    }
    

    See http://hotgazpacho.org/2010/09/testing-pattern-factory-method-on-base-test-class/

    Don't know if abstract test class inheritance is supported by all unit test frameworks though (I think NUnit does).