I am trying to test a class that calls methods in other classes. I need to isolate this class from the other ones and let the method being called return certain values (that I choose) to this class. The most important thing is that I don't want to change any source code being tested.
Example :
===========
class ClassUnderTest{
public:
int FuncUnderTest(){
HelperClass obj;
int x = obj.HelperFunction();
return x*2;
}
};
class HelperClass{
public:
int HelperFunction(){
---------
---------
}
};
Now I want to test ClassUnderTest in isolation, without calling HelperClass. I need a way to overwrite HelperFunction() to return a specific value during unit testing but without changing the code of any of the 2 classes.
Is it possible to do so with changing the code? How?
Simple: Exclude your original HelperClass from your project and add a mockup that will provide the test values. Once you are done testing ClassUnderTest, you can remove the mockup and restore the original HelperClass class.