chevron-thin-right chevron-thin-left brand cancel-circle search youtube-icon google-plus-icon linkedin-icon facebook-icon twitter-icon toolbox download check linkedin phone twitter-old google-plus facebook profile-male chat calendar profile-male
+2 votes

I am working on a C++ unit test. I have a  production code like below.

I am having trouble for redirecting a method call with a derived class which inherits from a base class as protected.

I want to call MockMethodA on anotherObj. (anotherObj is an instance of a concrete class in my unit test project) when methodA is called inside methodB.

But It does not work. It calls methodA of BaseClass. I tried both WHEN_CALLED and PRIVATE_WHEN_CALLED macros.

WHEN_CALLED(d->methodA()).DoMemberFunctionInstead(&anotherObj, MockMethodA);

PRIVATE_WHEN_CALLED(d, methodA).DoMemberFunctionInstead(&anotherObj, MockMethodA);


//production code. It cannot be changed.

class BaseClass




virtual ~BaseClass(){};

virtual void methodA()


cout << \"methodA\" << endl;



class DerivedClass :protected BaseClass





void methodB()


cout << \"methodB\" << endl;



asked by Levent (620 points)

Please log in or register to answer this question.