Search code examples
c++unit-testingtestinggoogletestgooglemock

Google Test Framework: is better to use shadowing or virtual methods?


In the example below, I want to unit test class A in order to verify that when A::request is called, B::response() is called as well:

class A
{
public:
    void request()
    {
        m_b.response();
    }

private:
    B m_b;
};

class B
{ 
public:
    void response();
};

In order to do that, class B has to be mocked:

class MockB : public B
{
public:
    MOCK_METHOD0( response, void());
};

So that the test will contain:

class TestA : public A
{
    ...
};

...
EXPECT_CALL( m_b, response( ) ).Times( 1 );
request( );
...

The question is: how to "inject" MockB as a replacement of B m_b?

First tecnique: Create a ShadowB class that redirects the method call to class MockB. This requires the original code to be in an external binary, but does not require any change in the actual code.

Second tecnique:

  • Making B::response virtual
  • Changing B m_b to a std::unique_ptr<B> m_b
  • Replace m_b value with an instance of class MockB during the test setup

Second approach means more code change and I'm not sure about its pro.


Solution

  • The correct way of solving your problem is the second technique. More generally, it is very hard to 'retro-fit' unit testing code onto components that were not designed with it in mind - you almost always need to significantly modify the code being tested.

    Using virtual functions to substitute Mock callback objects in place of real ones, both inheriting from a common interface is a very common approach. Another alternative is to have your classes being tested be template classes, and replace their template parameters with Mock objects.