Testing APIs

Mocks — testing without the real API

A mock is a pretend API that answers with prepared responses. When to use one, what it can and can't tell you, and how to keep mocks honest.

Sometimes the API you need isn't there: it isn't built yet, it's down, it's slow, it charges per call, or it belongs to another company and you can't create test data in it. A mock is a stand-in: a small server that answers requests with prepared answers, so everything that depends on the API can keep going.

Mocks, stubs, fakes

People use these words loosely. Roughly:

  • a stub answers fixed responses — "GET /orders/1 always returns this";
  • a mock is a stub that's also checked — "was it called, and with what?";
  • a fake is a working, simplified version — an in-memory shop that really stores orders.

In API work, "mock server" usually means the first: a server that answers each request with an example response. That's what this lesson is about.

When a mock helps

  • Building against an API that doesn't exist yet. The frontend team and the API team agree on the contract (an OpenAPI file); the frontend builds against a mock made from it, and switches to the real API when it's ready. Neither team waits for the other.
  • Testing the hard cases. A real payment API rarely declines on demand. A mock can answer 402 Payment Required, a 500, an empty list, or a 10-second delay — whenever you want — so you can test how your app handles them.
  • Cutting a dependency out of a test. A test of your API shouldn't fail because someone else's API had a bad afternoon. Mock the outside service, and your test checks only your code.
  • Demos and workshops, without real systems or data.

What a mock can't tell you

A mock only ever says what you told it to. So:

  • it can't tell you the real API works — only that your side handles the answers you prepared;
  • it can't tell you about performance — a mock answers in a millisecond; the real thing may not. Never load-test a mock and conclude anything about the real API;
  • it can drift — the real API changes, the mock doesn't, and your tests stay green while production breaks.

Keeping a mock honest

  • Make it from the contract, or from real answers. An example generated from the OpenAPI file, or saved from a real response, is far more likely to be right than one typed from memory.
  • Check the real API against the same contract. Run schema checks against the real API too. If both pass the same schema, the mock and the real thing agree on the shape.
  • Use realistic examples — real-looking ids, long names, empty and full lists — not "string" and 0.
  • Prefer the real API when you can. A mock is a tool for when the real one isn't available, not a replacement for testing it.

Dynamic answers

Good mock servers can fill values into answers: the id from the path (/orders/A-77 answers with "id": "A-77"), a fresh unique id for each create, the current time. That makes a frontend built against the mock behave more like it will against the real thing.

Key takeaways

  • A mock is a pretend API answering with prepared examples — for building early, testing hard cases, and cutting out dependencies.
  • It can't tell you the real API works or how fast it is — and it can drift from the real thing.
  • Make mocks from the contract or real answers, and check the real API against the same contract.

In the app: Mock server

Next: API tests in a pipeline

A question these lessons don’t answer? Write to support@apitestcenter.com — we add lessons for the questions people ask.