Mocks, stubs and fakes

A test double replaces a real dependency in a test. A stub returns canned answers and checks nothing. A mock is a stub that also records how it was called, so the test can assert the interaction happened — "charge was called once with 20". A fake is a working, simplified implementation, such as an in-memory database or an API that keeps state.

How it works

Stubs suit tests that only need the dependency to answer. Mocks suit tests where the call itself is the behaviour being verified. Fakes suit tests that exercise sequences — create, then read, then update — where canned answers would have to anticipate every step. In HTTP testing, all three are often loosely called "mock APIs".

The misconception

That more mocking means better isolation. Over-mocked tests assert on how code is written rather than what it does, and break on every refactor while missing real integration bugs. A fake that behaves like the real service often catches more with less setup.

Try it here

This playground is a fake in the strict sense: writes persist in your sandbox, so a POST is visible to a later GET, versions advance, and stock runs out — behaviour a stub cannot have.

Sources

Other terms

All glossary terms · Testing techniques · All 39 mock REST APIs

Last updated