I just started working on unit testing (using BOOST framework for testing, but for mocks I have to use Google Mock) and I have this situation :
class A
{
static int Method1(int a, int b){return a+b;}
};
class B
{
static int Method2(int a, int b){ return A::Method1(a,b);}
};
So, I need to create mock class A, and to make my class B not to use real Method1 from A class, but to use mock.
I'm not sure how to do this, and I could not find some similar example.
You could change class B into a template :
and then create a mock :
Before every test, initialize the static mock object
MockA::mock
.Another option is to instead call directly
A::Method1
, create a functor object (maybe std::function type) in class B, and call that in the Method2. Then, it is simpler, because you would not need MockA, because you would create a callback to MockCalc::Method1 to this object. Something like this :and to initialize it :