Introduction
This article discusses the difference between mock, stub, and spy. We'll show what the framework offers in terms of interaction-based testing.
Spock is a Java and Groovy testing framework that helps automate the manual testing of software applications. It offers its mocks, stubs, and spies and built-in capabilities for tests that would otherwise necessitate the use of third-party libraries.

Mock
Mocks are objects that keep track of method calls. The dynamic wrappers for dependencies used in the tests were referred to as this. It's used to keep track of and verify how Java classes interact. The test doubles' most powerful and versatile form is known as a mock.We employ a mocking technique named mock().
The fundamental benefit of mocks is that they provide complete control over the behaviour of mimicked objects. Mock objects are frequently used for behaviour testing. Checking the right methods and pathways that are applied to the objects is referred to as behavior.
The functions of using mocks in software testing include simulating behavior or responses of dependencies, isolating code under test from external systems, and enabling controlled testing of specific scenarios without relying on real components or external resources. Mocks facilitate faster, more focused, and deterministic testing by providing predictable responses and reducing dependencies on complex or unreliable external systems.
Mocks are often constructed using a library or mocking framework such as Mockito, JMock, or EasyMock. It's used to test a huge number of tests when stubs aren't enough. One of the most important features of mock is that it allows us to check how many times a certain method is called.
The use of mock() is demonstrated in the following code snippet.
#include <gtest/gtest.h>
#include <gmock/gmock.h>
// Define a simple interface
class DatabaseInterface {
public:
virtual int getValue(int key) const = 0;
};
// Define a mock implementation of the interface
class MockDatabase : public DatabaseInterface {
public:
MOCK_CONST_METHOD1(getValue, int(int key));
};
// Function under test
int fetchData(DatabaseInterface& db, int key) {
return db.getValue(key);
}
// Test case using mock
TEST(MockTest, FetchDataTest) {
MockDatabase mockDb; // Create a mock object
EXPECT_CALL(mockDb, getValue(42)).WillOnce(testing::Return(100)); // Define behavior of mock
int result = fetchData(mockDb, 42); // Call function under test
EXPECT_EQ(result, 100); // Verify result
}
int main(int argc, char** argv) {
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
Explanation: We define a simple interface DatabaseInterface with a method getValue() to retrieve values from a database. We create a mock implementation MockDatabase using Google Mock (gmock) library, which overrides the getValue() method. In the test case FetchDataTest, we create an instance of MockDatabase, define the expected behavior using EXPECT_CALL, and specify the return value. We then call the fetchData() function with the mock object as a parameter and verify the result using EXPECT_EQ.





