Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google Mock: Is it ok to use global mock objects?

In all the documentation about gmock I always find the mock object to be instantiated inside a test, like that:

TEST(Bim, Bam)
{
    MyMockClass myMockObj;
    EXPECT_CALL(MyMockObj, foo(_));
    ...
}

So, the object is created and destroyed per test. I believe it's also perfectly fine to create and destroy the object per test fixture. But I'm wondering if it's also ok to have a file-global instance of the mock object, like that:

MyMockClass myMockObj;

TEST(Bim, Bam)
{
    EXPECT_CALL(MyMockObj, foo(_))
    ...
}

I tried it and I have absolutely no problems so far, it all seems to work fine. But maybe I should be aware of anything? Just because I stumbled about this question, where the only answer states:

... the problem is that you're instantiating a global instance of FooMock. Googlemock/googletest expect the mock to be defined either within the body of the test, or within a test fixture class.

But I could not find anything in the documentation or anywhere else that confirms this (did I overlook it?).

Thanks, Georg

PS: The reason why I need to use a global mock instance would be the topic of another discussion (see this posting of mine).

like image 257
Georg P. Avatar asked Aug 28 '15 08:08

Georg P.


People also ask

What is the goal of using mock object for mocking?

Using mock objects allows developers to focus their tests on the behavior of the system under test without worrying about its dependencies. For example, testing a complex algorithm based on multiple objects being in particular states can be clearly expressed using mock objects in place of real objects.

What objects can be mocked?

Mock objects are simulated objects that mimic the behavior of the real ones. Typically you write a mock object if: The real object is too complex to incorporate it in a unit testing (For example a networking communication, you can have a mock object that simulate been the other peer)

Does Gtest include Gmock?

gMock is bundled with googletest.

Why is Gmock used?

gMock is used for creating fake objects in order to remove external dependencies for effective testing.


1 Answers

You can, but it is not a good idea.

Doing such a thing is violate the isolation principle of UT. This violation may cause an unexpected failure/pass in your tests.

Gtest uses the destructor of the fake objects to verify that the expectation occurred, this is the reason behind the expectation that each fake object will create and release in the body of the test, or within a test fixture class.

If you make the fake object global then it won't release at the end of each UT, then the verification won't execute and the test will pass even when it should fail. more over some of your UTs may fass/fail when you execute all your tests together; in one test you expect the method x won't call and in the other you expect that the method will call; in one UT you expect the method x will call 3 times, but the method was call twice in the test + one in other test(the test should fail but it won't...)

So the bottom line you should never use a global mock unless this global mock is being in use only to prevent null pointer(you didn't set a behaviour..)

like image 152
Old Fox Avatar answered Sep 21 '22 02:09

Old Fox