Try something more like:
#include <iostream>
#include <gtest/gtest.h>
#include <gmock/gmock.h>
class GTEST_static_class {
public:
virtual void display() { std::cout << "inside the GTEST_static_class:: display\n"; }
virtual ~GTEST_static_class() {}
};
class MockGTEST_static_class : public GTEST_static_class {
public:
MOCK_METHOD0(display, void());
};
class GTest_static_example : public ::testing::Test {
public:
void call_display(GTEST_static_class *instance) {
instance->display();
std::cout << "display called from GTest_static_example\n";
}
};
int main(int argc, char * argv[]) {
::testing::InitGoogleTest(&argc,argv);
return RUN_ALL_TESTS();
}
TEST_F(GTest_static_example, case1) {
MockGTEST_static_class mock;
EXPECT_CALL(mock, display()).WillOnce(::testing::Return());
call_display(&mock);
}
You need to pass the actual instance of the class upon which you make the expectation. Also, you need to make void display();
virtual if you want the mock to be able to override it. Forgetting to add virtual
unfortunately doesn't generate any compiler warnings or errors - it' just something you have to watch out for. You should also make GTEST_static_class
's destructor virtual to avoid slicing.