Egor Urvanov

Mock

Testing · mock object, stub, fake, test double

A mock is an imitation of a real dependency, such as a database or a third-party payment service, that is plugged into a test or onto a stand in place of the real one. The system calls it as if it were real, and it replies with prepared answers, so the test is fast, free and does not touch anyone’s data. For example, instead of a real card payment the mock always answers “paid”. Mocks are most often found in unit tests, where they replace the dependencies of the part under test. The word “mock” is often used for any such imitation, although in the strict sense there are several kinds. Mocks are sometimes written by hand for a specific test and sometimes taken ready-made: standard tools exist in almost every language. For example, the unittest.mock module ships in the Python standard library, and WireMock imitates HTTP services.

the systemcalls it as if it were realin productionreal paymentreal money, slow, someone’s datain a test«ok»payment mockalways “paid”: fast, free, touches nothing● model, people, systems, data  ● agent and harness  ● risk, stop, boundaryIn a test a mock replaces the real dependency: it answers with canned replies, fast and free

Mock, stub and fake

StubMockFake
What it doesreturns a prepared answerchecks how it was calledreally works, only simpler
Exampleexchange rate is always 90check: payment was requested oncein-memory database

Frequently asked questions

What is a mock in testing?

An imitation of a real dependency, such as a database or a third-party service, that replies with prepared answers while a test runs.

How is a mock different from a stub and a fake?

A stub only returns a prepared answer. A mock also checks how it was called. A fake is a simplified but working implementation, such as an in-memory database. The split is not universally accepted, and “mock” often means any of the three.

Why use mocks on a stand?

They are cheaper and faster than copies of real services and do not send real emails or payments. The price is that the stand is a little further from reality.

How do mocks help against flaky tests?

A mock removes the dependency on an outside system that answers fast, slow or not at all, so the test result no longer depends on it.

Are mocks written by hand or taken ready-made?

Both happen. A simple mock is written by hand for one test. For typical tasks ready-made tools are used: unittest.mock ships in the Python standard library, Mockito replaces objects in Java, WireMock imitates HTTP services, jest.fn() does the same in JavaScript.

Source: Wikipedia: Test double

A ready-made mock: unittest.mock in the Python standard library Another example: Mockito, mocks for Java Another example: WireMock, imitation of HTTP services Another example: jest.fn(), mocks in JavaScript

Where it appears · 1

Related terms

Whole glossary

© 2026 Egor Urvanov