Other meanings of Mock object
Software testing
A mock object is a simulated object used in software testing to verify interactions and behavior. It stands in for a real dependency, records how the system under test uses it, and can be configured to return values or raise errors without invoking the dependency’s full implementation.
Mock objects test how a unit communicates with its collaborators rather than reproducing the collaborators themselves. A test can replace a database gateway, HTTP client, message publisher, clock, or file service with a mock and then verify that the system under test made the expected calls.1
The term belongs to the broader family of test doubles, which also includes stubs, fakes, spies, and dummies. A stub supplies predetermined responses; a fake provides a lightweight working implementation; a spy records activity for later inspection; a mock generally combines configurable behavior with explicit interaction expectations.2 In practice, testing libraries sometimes use “mock” as a broad label for several of these forms.
A mock is typically configured before the test and verified after the system under test performs an operation. The configuration may specify a return value, an exception, a callback, or a rule for matching arguments. Verification then checks whether a method was called, how many times it was called, with which values, and sometimes in what sequence.3
Frameworks implement these behaviors through generated proxy objects, subclassing, bytecode instrumentation, or language features such as dynamic dispatch. Java’s Mockito, for example, supports stubbing and verification of interactions, while Python’s standard unittest.mock library provides Mock, MagicMock, patching, call inspection, and autospeccing against a real interface.4 5
A typical test therefore has four stages: create the mock, define its responses, execute the production operation, and assert the recorded interaction. The test should also check the externally meaningful result, not merely that a call occurred.
Mocks make tests fast, deterministic, and independent of services that are slow, costly, unavailable, or difficult to reproduce. They are particularly useful for failure paths, such as a timeout from a payment service or an exception from a repository, because the test can trigger those conditions directly. They can also expose an unintended dependency on a collaborator’s API.
The main risk is over-specification. A test that asserts every internal call, exact call order, or incidental argument can fail after a harmless refactoring, creating a brittle coupling between tests and implementation. Excessive mocking may also produce tests that pass while the real components do not integrate correctly; integration and contract tests remain necessary for boundaries such as databases and network protocols.2
Mocks are least useful when the dependency is simple, when a small fake is clearer, or when the behavior being tested is fundamentally an end-to-end interaction.
Mocking can replace more than ordinary methods. Modern libraries can intercept constructors, static methods, properties, asynchronous calls, iterators, context managers, and magic methods, although these facilities often depend on language or runtime support.4 Python’s autospec feature is a particularly practical safeguard: it constrains a mock to the signature or attributes of the object it represents, helping tests detect misspelled methods and invalid calls.
Partial mocks, sometimes called spies or partial doubles, invoke real behavior for some operations and substitute others. They can be useful around legacy code but may conceal complicated state interactions. Mocking time, randomness, environment variables, and filesystem access also requires care because patching the wrong import location can leave the real dependency active. These edge cases explain why test-double guidance emphasizes clear seams, narrow interfaces, and behavior-oriented assertions rather than indiscriminate replacement of every collaborator.1
Terminology varies among testing communities: some frameworks call any programmable substitute a mock, while stricter usage distinguishes mocks by their interaction expectations.
Help improve the encyclopedia. Reports go straight to the site manager.