Mock Object
A mock object is a simulated version of a real dependency, such as a database, API, or external service, used in automated tests to replace that dependency with predictable, controllable behavior.
Definition
A mock object is a simulated version of a real dependency, such as a database, API, or external service, used in automated tests to replace that dependency with predictable, controllable behavior.
Overview
When testing a piece of code that depends on something slow, unreliable, or external — a network call, a database, the current time, or a paid third-party API — a mock object stands in for the real thing during the test. Instead of actually hitting a database, code under test interacts with a mock that returns pre-programmed responses, letting a unit test run quickly, deterministically, and without needing that external system to be available or configured at all. The term “mock” is often used loosely to cover a family of related concepts with more precise distinctions: a stub simply returns canned responses without checking how it was called; a mock, more strictly, also verifies that it was called correctly, with the right arguments and the right number of times, failing the test if not; a spy wraps a real object while recording how it was used; and a fake is a lightweight, working implementation, such as an in-memory database, that behaves like the real thing but without its full complexity. Frameworks like Jest's built-in mocking, unittest.mock in Python, and Mockito for Java provide tooling for creating all of these variants. Mocking is powerful but has real trade-offs: tests that mock too aggressively can pass even when the real integration between components would fail, since the mock's behavior may drift from what the real dependency actually does over time. This is one reason integration tests, which use real dependencies, remain necessary alongside heavily mocked unit tests — mocks give fast, isolated feedback about a single unit's logic, but they cannot guarantee the real system behaves the same way when everything is wired together for real. It is often mentioned alongside End-to-End Testing in this space.
Key Concepts
- Simulates a real dependency with predictable, controllable behavior
- Lets unit tests run quickly without needing real external systems
- Distinguished from related concepts: stubs, spies, and fakes
- Strict mocks verify they were called with the correct arguments
- Common tooling includes Jest mocks, unittest.mock, and Mockito
- Removes flakiness and slowness caused by real network or database calls
- Can mask real integration bugs if a mock's behavior drifts from reality
Use Cases
Frequently Asked Questions
From the Blog
Object-Oriented Programming in Python Explained Simply
A comprehensive guide to object-oriented programming in python explained simply — written for learners at every level.
Read More ProgrammingObject-Oriented Programming in Python: A Practical Guide
OOP is how Python codebases stay organised as they grow. This guide explains classes, inheritance, encapsulation, and polymorphism with real examples — and tells you honestly when to use OOP and when plain functions are the better choice.
Read More