Sep 27

Gmock – Cross Platform Mock Framework

Gmock – Google Mock – Cross platform mock framework


Theoretical Background:


Gmock is a cross platform framework for writing mocks for tests in c++

It supports Android, Windows, Mac, IOS and more.


Gmock is not a standalone testing framework, it is used only for setting mock functionality.
It complements the gtest framework of google and it also takes dependency on gtest, however you don’t have to use it with gtest and can choose another framework of your choice should you prefer to integrate the other framework with gmock.


gmock main page:



What is this framework used for:

Gmock is a framework that helps you create mocks of classes in a relative trivial manner that shortens your development time, so instead of the overhead of developing mocks by yourself and setting implementation for classes and methods, it lets you concentrate on the test content and set the expected results of mock classes using simple Macros that are part of the framework’s API.


In order to set a mock for the Test:

Say we have the following abstract class Adder:


    class Adder


        virtual int Add(int i, int x) = 0;



That we want to have a mock implementation for it.

In order to do so we will define a mock class that inherits from Adder:


namespace LIOR_TEST
    namespace Test
        namespace Mock
            class MockAdder : public Adder

                MOCK_METHOD2(Add, int(int, int));


The format for declaring the mock methods is as follow:

MOCK_METHOD<number_of_argurments>(<name_of_function>, <return_type>(<parameter_type,…,>));


Now that we set the mock class, gmock provides API to help us test the method, say we want test a Calculator class that uses an Adder class:

We will set the MockAdder as the instance that the Calculator class will use and now we can set the result of the Add method of Adder that Calculator uses to return what we would like during runtime, we can even check if the method Adder was actually called, how many times it was called and more.


For example, to set the MockAdder to return a fixed result:

We will create an instance of the MockAdder:


shared_ptr m_mockAdder;


In the SetUp method we will set the expected result by:


m_mockAdder = make_shared();
EXPECT_CALL(*m_mockAdder, Add(::testing::_, ::testing::_)).WillRepeatedly(::testing::Return(5));


And in the test we will simply call a method of Calculator that is expected to call the Add method of the Adder, and the result of the method call would return the value 5 constantly as we defined:


       TEST_F(CalculatorTest, AdderTestExampleUsingTheMockAdder) {
            Calculator calculator;

            //The MockAdder always returns the result 5.

            EXPECT_CALL(*m_mockAdder, Add(::testing::_, ::testing::_)).Times(2);

            int result = calculator.AddTwice(1, 1);

            //Since we use the MockAdder we will get the result 10 and not the expected result of 4
            EXPECT_EQ(4, result) &lt;&lt; "Expected result 4 but using the mock got: " &lt;&lt; result;


Note: of course there is no sense in returning the constant value 5 in an Add method, this example is only to demonstrate how to use gmock.


How does it work?

The EXPECT_CALL.Times(2) command is evaluated at destruct time of the mock, so if by the time of the destruction of the mock, the method was called not exactly twice, the test would fail. So it is important to check that we don’t leak the Mock instance or the destruction will not be called and the verification of EXPECT_CALL.Times would not be checked.


EXPECT_TRUE(i == 1) << "i does not equal 1";


There are more Macros in gmock, the main Macro is EXPECT_CALL and the general syntax for it is:


EXPECT_CALL(mock_object, method(matchers))


In conclusion:

In this article I gave a very basic coverage to the gmock framework, if you would like to know more about the framework, you can read the CookBook in the following link:



I found gmock very simple to use, especially with gtest.


Good Luck !

Leave a Reply

Your email address will not be published.

אתם יכולים להשתמש באפשרויות ותגי ה-HTMLהבאים: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>