Services

Resources

Company

API mocking

API mocking replaces a real API with a stand-in that returns predefined responses. A developer can build against a payments or user service that is not ready yet, and tests can cover error cases that are hard to trigger for real. Mocks are often generated from an OpenAPI specification so they match the agreed contract.

How it works

A mock server listens on a URL and matches each request by method, path and sometimes body, then returns a stored response with a status code, headers and a payload.

For example, while the payments team is still building, a mock answers POST /payments with 201 {"status": "paid"}, and with 402 {"error": "card_declined"} when the amount is 0. The checkout team can now write and test both paths. When the real service ships, the same tests run against it.

Why it matters in production

Mocks drift. If the real API adds a field or changes an error code, tests against an old mock keep passing while production breaks. Generate mocks from the same spec the provider implements, or add contract tests, and run a smaller set of tests against the real service before release.

See also

On the One2N blog

References

  1. Postman: Mock APIs overview

  2. Martin Fowler: Mocks aren't stubs

Comic about API mocking: Kai builds checkout but the payments API ships in 3 weeks, so Kai runs a mock server from the agreed payments.yaml spec; the mock returns 201 paid for amount 4999 and 402 card_declined for amount 0, so both cases are tested today; in week 3 the real API returns pending with a redirect URL and checkout breaks in production; the fix runs the same contract tests on mock and real API.