I am trying to write unit tests in C++ and am facing an issue with creating mock objects for an external dependency using Fakeit. So we have a class similar to the following:
class A
{
int test_method()
{
B obj;
return obj.sendInt()
}
};
class B
{
int sendInt()
{
return 5;
}
};
Now let's say I want to write a unit test for test_method() of class A. When we call obj.sendInt() I want to mock it and return a different value. I tried using fakeit but was not able to arrive at a solution.
I know this will be solved if we try to do a dependency injection of B via constructor or in setter methods, but I don't want to do it as it would take some refactoring in existing consumers of A.
For a similar scenario in Java, I would use PowerMockito and achieve the same using PowerMockito.whenNew
B mock = Mockito.mock(B.class);
PowerMockito.whenNew(B.class).withAnyArguments().thenReturn(mock);
Mockito.when(mock.test()).thenReturn(2);
A obj=new A();
assertEquals(obj.test(), 2);
The easiest way would be to use dependency injection. I don't think there's anything similar to
PowerMockitofor C++ (e.g. it's impossible to mock static methods/functions in a similar manner asPowerMockitoallows for java).If the issue is only with the dependency injection via ctor or setter methods, consider using hi-perf dependency injection, i.e. inject mock using templates.
If the
class Acannot be modified at all but you own theclass B, consider movingclass Bto a separate static library: one for production (say,libBprod) and one for testing (libBtest). In production you can link against thelibBprodand in tests againstlibBtest. InlibBtest, you can makeclass Ba singleton under the hood. This is quite a lot of work though.If both
class Aandclass Bcannot be modified, then I'm out of ideas - you need to refactor some part of the code somehow.