My question is the same as Mockito: Mock private field initialization but for Google Mock framework. In a nutshell:
class Target {
private:
Person person = new Person();
public:
void testMethod() {
person.someMethod();
}
};
How can I mock the person
instance while making unit tests for Target
class?
A non-answer here: simply don't do it this way.
Your problem is the call to
new
here. Thing is: that makes testing hard, and it also creates a very tight coupling between theTarget
and thePerson
class.The default alternative is: provide a factory to the
Target
class that createsPerson
objects for you.By going for that solution, you
new
And unless I am misreading the documentation, mocking calls to
new
isn't possible with C++ mocking anyway.