Oscar Forner Martinez bio photo

Oscar Forner Martinez

I am software engineer at VCA Technology. Learning Linux Kernel, passionate about GNU/Linux and OSS, enjoying algorithms and data structures and coding in C and C++.

Email Twitter Google+ LinkedIn Github

This post is a continuation from a previous post called Unit test with Google Test for C++, but in this post we are going to use Google Mock, that extends the functionality of Google Test.

Google Mock is one of the available frameworks for C++ to mock objects in unit tests. In this example I will use the same technologies as for the previous one: CMake to configure the project and build it, for the dependency manager I will use the new and shiny conan.

All the code and configuration files used in this post are available in this repo in GitHub.

Why do we need to mock objects?

To answer that question I will introduce the two classes I will use during this post.

The first class is called Producer and it will extract the domain of a given URL:

The second class is called Consumer and it will calculate the level of the domain of a given URL:

In this case we want to create a unit test for the Consumer class, but we need to implement a Producer class for that, right? Well, no. We do not need an implementation of the Producer class because we can mock it. Moreover, in that case the unit test would not be only for the Consumer class, but for the Producer too, and that is not what we are looking for.

Unit test with Google Mock

The following unit test is written using the Google Mock framework:

On the one hand, the ProducerMock class is inheriting from the Producer class. However, the interesting part is The line above is telling Google Mock to mock the method getDomainFromUrl from the Producer class and it will receive a const std::string by reference as parameter and it will return a std::string by value.

On the other hand, there is the EXPECT_CALL macro that receives two parameters: The first parameter is the mocked object and second one is the method that will be called and the parameter that will be passed. The result of the expansion of the macro will call a method called WillOnce that will make the mocked object answer once to the method. Finally, ::testin::Return(domain) specifies the return of that call, which will be domain.

The execution of the test will be the following:


Google Mock, or any mocking framework, is an important tool for developers because it allows to create unit tests for interoperability of different objects. Furthermore, you can create unit tests that are focused on a single class without worring if the class to which it depends is working properly.