26
votes

To be able to unit test my C++ code I usually pass the constructor of the class under test one or several objects that can be either "production code" or fake/mock objects (let's call these injection objects). I have done this either by

  1. Creating an interface that both the "production code" class and the fake/mock class inherits.
  2. Making the class under test a template class that takes the types of the injection objects as template parameters, and instances of the injection objects as parameters to the constructor.

Some random thoughts:

  • Until we have concepts (C++0x), only documentation and parameter naming will hint what to provide the class under test (when using templates).
  • It is not always possible to create interfaces for legacy code
  • The interface is basically only created to be able to do dependency injection
  • In the same way: templating the class under test is done only to enable dependency injection

What are your thoughts? Are there other solutions to this problem?

4

4 Answers

10
votes

With C++, there's another option - you give your mock classes exact same names as the real classes, and when linking your unit tests, just link them with mock object/library files instead of real ones.

6
votes

I think interface option is better, but one doesn't have to create common base class just for test. You can inherit your mock class from production class and override necessary methods. You'll have to make the methods virtual though, but that's how tools like mockpp work and they also allow automate this process a little bit.

3
votes

Templates will have slightly less performance penalties for runtime (less indirections, less calls, more inline optimizations), but will make you suffer a very high penalty for compilation times...

I think that for this purpose, interfaces are better (until we have concepts in C++0x TR1)... unless if you can't slow down some "bottleneck code". Interfaces are more dynamic and switchable at run-time.

Remember that you can construct your class with default injection objects (the real ones), but you can have factories that inject the mock ones on your tests... you don't even need to subclass.

2
votes

Don't know if it helps, but you can have template constructors:

struct Class_Under_Test
{
    template <typename Injected>
    Class_Under_Test()
    {
         ...

    // and even specialize them
    template <>
    Class_Under_Test <A_Specific_Injection_Class>
    {
        ...

Only the one that is actually used will get included.