We have the following problem: several classes that we cannot touch, but need a unit test, unfortunately, the classes are not designed for unit testing, so we create mock-ups for checking the code.
Example:
class SomeOtherClass { public: void foo2() { … } }; class ClassToTest { public: ClassToTest() {…} void foo1() { SomeOtherClass A.foo2(); } };
In the above example, we would like to test foo1()
, but it needs foo2()
, so we would like to make foo2()
belong to the layout of the object (in real life, these functions / classes are much more complicated and require interaction with hardware configurations, etc. ., thus, the need for mock objects / functions).
So far, we have done something similar, but it really is not optimal, because the code seems to have side effects for other unit tests.
class MockSomeOtherClass { public: foo2() { … }
Is there a better way to do this without changing the source classes (or with minimal changes)? We use CPPUnit for testing.
EDIT: added winapi tag for a clearer description of the environment.
source share