TDD Best Practices: Don’t Mock Others

On April 1, 2012, in Uncategorized, by derekgreer
This entry is part 2 of 2 in the series TDD Best Practices

Test Doubles play an important role in the practice of Test-Driven Development for establishing a controlled context, facilitating outside-in design, verifying component interaction, and providing overall test stability through isolation.  While isolating components from their dependencies is a Good Thing, some of the advantages of using test doubles and Test-Driven Development itself can be thwarted by substituting the wrong components.

One design principle which is particularly relevant to the practice of using test doubles (mocks, stubs, spies, fakes, etc.) is the Dependency Inversion Principle.  Stated simply, the Dependency Inversion Principle pertains to the decoupling of the stuff you most care about (like your domain layer) from the stuff you care least about (like your low-level infrastructure code and third-party libraries).  In relation to the practice of using test doubles, it’s the coupling to other people’s stuff that is most relevant.

When you provide test doubles for types you don’t own, this indicates that you have a Dependency Inversion Principle violation.  If you’re injecting concrete, abstract, or interface types for a third party dependency then that means your stuff can’t be used without their stuff.  While not ideal, you might ignore this design principle due your personal stance of: “They’ll pry framework XYZ from my cold, dead hands”.  Fair enough.  Nevertheless, there are a few other reasons why you might want to consider following this principle.

When using test doubles for third-party libraries, you make assumptions about how the third-party library works.  Perhaps the library you’re using has no bugs and you thoroughly understand all of its behavior, so you know that the behavior you are substituting behaves exactly like the real library will work in production once you put it all together.  If you aren’t confident of this, however, you might find that your design doesn’t interact with the third-party component in quite the way you imagined.  Let’s say it does though.  What about the next version?  When we design our systems using test doubles for third-party libraries, we create a false sense of security around the soundness and stability of our design.

Another problem caused by the use of test doubles for third-party components is the limitation it places on emergent design.  Coupling to third-party components will often lead to making design concessions to accommodate your dependencies rather than allowing your own design to emerge through the feedback received through the TDD process.

If we shouldn’t couple our design to third-party libraries (thereby removing the need to supply test doubles for third-party libraries), how then do we make use of such libraries and ensure our code works correctly?  The answer is to write component/unit level tests which guide the design of your own code which expresses its needs through its own interfaces (whose behavior is defined through the use of test doubles), and write integration tests which validate the behavior of adaptors which implement your design’s interfaces using the desired third-party components.

For example, you might design your components to rely upon an ILogger, IMapper, or IBus using test doubles to define their expected behavior and have integration tests which validate the implementation of these interfaces with log4net, AutoMapper, or ØMQ respectively.

In summary, don’t use test doubles for types you don’t own.  Instead, let any dependencies you take on be an outgrowth of your emergent design and provide integration tests to validate the expected behavior of any third-party libraries you use to facilitate the required behavior.

Series NavigationTDD Best Practices: Write Assertions First
Tagged with:  
  • Jon Davis

    This is is one reason why I am irritated that when using Unity the “best practice” is to use IUnityContainer rather than, say, IServiceLocator. Not that Service Locator pattern is more ideal than an explicit DI container with Register/Resolve (etc) but that the interface itself is proprietary. I like the idea of being able to swap out the DI container.
    Am I wrong?

    • derekgreer

      Regardless of which container you choose to use, references to a container are best localized within the infrastructure initialization part of your application. For instance, I typically create a class called Bootstrapper which is either invoked explicitly by the entry point of the application (Global.asax, main(), etc.) or invoked through WebActivator. This class (along with any facilitating initialization modules/initializers, etc.) is typically the only place I ever reference the container. As it pertains to the topic of this article, code which references the container is covered through automated acceptance tests, so there is never a need to mock a container for any component/unit level tests.

      As far as the Service Locator pattern is concerned, while your question focuses on whether it’s best to use an abstraction to the container (e.g. IServiceLocator) or to use the container directly, it should be noted that both use cases are instances of the Service Locator pattern. In general, you should favor dependency injection over the Service Locator pattern (i.e. constructor injection) as it decouples your components from the specific mechanism of how dependencies are obtained by the component and it makes explicit the dependencies of a given component (i.e. the constructor tells you what behavior the class requires).

      • Jon Davis

        No, I would only use this where constructor injection isn’t available (i.e. obviously app startup, but I recently ran into other places)

  • derekgreer

    Regardless of which container you choose to use, references to a container are best localized within the infrastructure initialization part of your application. For instance, I typically create a class called Bootstrapper which is either invoked explicitly by the entry point of the application (Global.asax, main(), etc.) or invoked through WebActivator. This class (along with any facilitating initialization modules/initializers, etc.) is typically the only place I ever reference the container. As it pertains to the topic of this article, code which references the container is covered through automated acceptance tests, so there is never a need to mock a container for any component/unit level tests.

    As far as the Service Locator pattern is concerned, while your question focuses on whether it’s best to use an abstraction to the container (e.g. IServiceLocator) or to use the container directly, it should be noted that both use cases are instances of the Service Locator pattern. In general, you should favor dependency injection over the Service Locator pattern (i.e. constructor injection) as it decouples your components from the specific mechanism of how dependencies are obtained by the component and it makes explicit the dependencies of a given component (i.e. the constructor tells you what behavior the class requires).