You can, but it's not a good idea.
Doing such a thing violates the UT isolation principle. This violation may lead to an unexpected failure / skip in your tests.
Gtest uses a fake object destructor to verify that an expectation has occurred, which is the reason that each fake object will be created and released in the test body or in the test device class.
If you create a fake global object, it will not be released at the end of each UT , then the check will fail and the test will pass even if it should fail. more than some of your UTs may have fass / fail when running all your tests together; in one test, you expect method x not call, and in another you expect the method to call; in one UT, you expect method x to call 3 times, but the method was called twice in the test + one in the other test (the test should fail, but it will not ...)
So, on the bottom line, you should never use a global mock unless this global layout is used only to prevent a null pointer (you haven't set a behavior ..)
source share