No, this is not possible without changing the sources or casting your own version of foo() related to the executable code.
From the FAQ in GoogleMock it says
My code calls a static / global function. Can I mock this?
You can, but you need to make some changes.
In general, if you need to mock a static function, it means that your modules are too tightly coupled (and less flexible, less reusable, less verifiable, etc.). You are probably better off defining a small interface and calling a function through that interface, which can then be easily ridiculed. At first it works a little, but usually pays for itself quickly.
This Google Testing post blog talks about it superbly. Check this.
Also from Cookbook
Docking free features
You can use Google Mock to bully a free function (i.e. a C-style function or a static method). You just need to rewrite your code in order to use the interface (abstract class).
Instead of directly calling a free function (for example, OpenFile), enter an interface for it and create a specific subclass that calls the free function:
class FileInterface { public: ... virtual bool Open(const char* path, const char* mode) = 0; }; class File : public FileInterface { public: ... virtual bool Open(const char* path, const char* mode) { return OpenFile(path, mode); } };
Your code must talk to FileInterface to open the file. Now it's easy to mock a feature.
This may seem like a lot of trouble, but in practice you often have several related functions that you can put in the same interface, so the syntax overhead for each function will be significantly lower.
If you are concerned about the performance overhead of virtual functions, and profiling confirms your concern, you can combine this with a recipe for ridiculing non-virtual methods.
As you mentioned in your comment that you really provide your own version of foo() , you can easily solve this by having a global instance of another mock class:
struct IFoo { virtual A* foo() = 0; virtual ~IFoo() {} }; struct FooMock : public IFoo { FooMock() {} virtual ~FooMock() {} MOCK_METHOD0(foo, A*()); }; FooMock fooMock;
Remember to clear all call waiting after running each test case.