I'm trying to make fun of a function in C, mocking works great when the function and its caller function are defined in different files. But when both functions (the function itself and its caller) are defined in one file, the mocked function is not called.
Case 1:
#include <stdio.h>
int __wrap_func() {
printf("Mock function !!!\n");
}
int myTest() {
return func();
}
int main() {
myTest();
return 0;
}
#include<stdio.h>
int func() {
printf("Original function !!!\n");
}
Case 2:
#include <stdio.h>
extern int myTest();
int __wrap_func() {
printf("Mock function !!!\n");
}
int main() {
myTest();
}
#include<stdio.h>
int func() {
printf("Original function !!!\n");
}
int myTest() {
return func();
}
Code Compilation Command: gcc -Wl, - wrap = func test.c file.c
In Case 1 . Mock function !!!
In Case 2 . Original function !!!
In case 2, the mocking function is not called. I am looking for a solution in which I can make fun of a function, even the calling function and the called functions are in the same file.
source
share