Given a C library (liblegacy.a
) that contains:
function_legacy1()
function_legacy2()
function_legacy3()
...
function_legacy500()
and a C binary (mybin
) which links against liblegacy.a
:
function_binary1() {
function_legacy1();
function_legacy2();
function_legacy3();
}
function_binary200() {
function_legacy500();
}
and mybin
is partially tested with Google Test framework (in progress).
The technical debt is high and it will be a big work to test mybin
and/or liblegacy.a
.
As a step to remove this debt, I like to start implementing a test for function_binary1
without impacting the rest. My idea would be to mock only the 3 functions used by it (function_legacy1
, function_legacy2
, function_legacy3
) and keep linking against the lib so that I don't have to split the .c/.h files to have only the interesting part in the translation unit.
A first approach would probably to make a dynamic shared library that contains the 3 functions, and use LD_PRELOAD to override these ones at runtime.
Since I'm new with GMock, maybe we can do that in a better way directly with this framework. Is is possible to mock only some functions of an external lib with GMock to avoid refactoring in this case?
Note: This question is somehow related to, but answers are not very clear to me