I am currently trying to write the unit test for some legacy code. In one of my function, it calls another function. According to the return value of the inner function, three branching conditions are present. The code is written in C++ and I am using cxxtest framework. How can I manipulate the return value of the inside function without going into the implementation of it so that I can pass through all the branching condition? I prefer to avoid dependencies between the functions. Is there any way to do this?
I found a solution. I have created a new header file called new_header.hpp. Added a function with the same syntax like socket function into the file like this :
int socket(int domain, int type, int protocol)
{
if(domain == CONST1)
return -1;
else if(domain == CONST2)
return 1;
}
Then in the cmake file, I have created a library to run UT. I added this new new_header.hpp with other required files as a dependency. Also, I have included this file in the header of my newly created UT file. Voila, by manipulating one of the values in the parameter I was able to mock the required functions. Adding the mocks for user-defined functions also work in same way.