This page introduces the core concepts of BonoboMock and walks through a basic example.
Prerequisites:
- Read What is BonoboMock and when should I use it? for an overview of the library.
- This guide assumes familiarity with GoogleTest and gMock. If you are new to GoogleTest/gMock, please read the linked documentation first.
BonoboMock uses runtime binary patching to intercept function calls at runtime. When you mock a function, BonoboMock replaces its entry point in memory so that calls are redirected to a gMock object. This approach allows BonoboMock to mock functions that traditional interface-based mocking cannot, such as free functions, static methods, and non-virtual member functions.
For a detailed explanation of the patching mechanism, gMock integration, and call interception flow, see Internals.
Every test that uses BonoboMock follows three steps:
BONOBO_MOCKEXPECT_CALL with BONOBO_MOCK_FUNCTION#include <bonobomock_api.h>
#include <gmock/gmock.h>
#include <gtest/gtest.h>
// A simple free function to mock
int add(int a, int b) {
return a + b;
}
TEST(Primer, MockFreeFunction) {
// Step 1: Declare the mock
auto mock = BONOBO_MOCK(&add);
// Step 2: Define expectations
EXPECT_CALL(*mock, BONOBO_MOCK_FUNCTION(_, _))
.WillOnce(Return(42));
// Step 3: Call the function -- it returns the mocked value
EXPECT_EQ(42, add(1, 2));
// The mock auto-restores when it goes out of scope
}
BONOBO_MOCK_FUNCTIONBONOBO_MOCK_FUNCTION is used inside EXPECT_CALL to specify gMock’s matchers for the function’s arguments:
_ matches any object), and the remaining matchers correspond to the function’s arguments.// Free function int sample(int a, int b):
// matchers map directly to arguments
EXPECT_CALL(*mock, BONOBO_MOCK_FUNCTION(arg1_matcher, arg2_matcher));
// Member function int MyClass::method(bool flag):
// first matcher is the object pointer
EXPECT_CALL(*mock, BONOBO_MOCK_FUNCTION(obj_ptr_or_wildcard, arg1_matcher));
See more examples in the appropriate sections of the Cookbook.
The mock object automatically restores the original function when it goes out of scope. You can also call mock->restore() to restore it manually.
The simplest way to declare a mock object is with auto:
auto mock = BONOBO_MOCK(&sample_function);
If your project must compile with C++03 (where auto is unavailable) or you need a mock as a GoogleTest fixture member (where auto cannot be used for member variables), you must declare the type explicitly. See Explicit Mock Object Types for more details.