liveBook · Manning

Mocking

As explained in The Design of Web APIs, Second Edition

An API simulator is a software representation that imitates an API's behavior before or apart from its production implementation.

An API mock is a simulation of an API that has not yet been developed. A basic mock exposes static data, letting designers and stakeholders call an endpoint rather than judge it only from a description. That interaction can reveal missing details, support design discussions, and let consumer teams try proof-of-concept applications early. section 19.1.2 section 6.1.2

Try an account endpoint before implementing it

Suppose an OpenAPI document defines GET /accounts. An OpenAPI-based mocking tool such as Microcks or Postman can return data drawn from the operation’s JSON Schema or examples. More detailed schemas and representative examples make the returned data more useful. Some tools also validate input, but their error responses may not match the intended API. section 19.6.1

A mock can become more realistic: templates can add random IDs or dates to a POST /transfers response; scripts can vary the response according to an input date; and some tools can retain a transfer so a later GET /transfers/123 finds it. Each behavior takes configuration or scripting, so greater realism is not automatically the best investment during design. section 19.6.2

Choose the smallest simulation that answers the design question:

  • Use a basic OpenAPI-based mock when static responses are enough to test how an API feels to call. section 19.6 section 19.6.1
  • Prefer an early implementation prototype when design evaluation needs realistic behavior. Start with a skeleton returning static data, then add logic as the design develops. section 19.6.2
  • Consider a complete mock later when developers need to work in isolation or API users need a demo environment; avoid building one merely to complete the design reference kit. section 19.6.2 section 19.1.7

To evaluate a proposed endpoint, define its operation and representative examples in OpenAPI, call a basic mock, and ask consumers whether the resulting requests and responses support their intended use. Add simulated behavior only when it will expose a design decision that static data cannot. section 6.1.2 section 19.6

How The Design of Web APIs, Second Edition covers it

How deep it goes
Explains static OpenAPI-based mocks and outlines templating, scripting, and saved data, but favors prototypes over elaborate design-stage mock configuration.
Examples
Illustrates static GET /accounts responses and POST /transfers behavior involving generated IDs, dates, and later retrieval.
What it assumes
Assumes an OpenAPI description for a basic mock and uses JSON Schema or examples to shape returned data.
What you can do after
Apply it via an OpenAPI-based mocking tool to try an undeveloped API and decide when a prototype better serves design evaluation.
How it connects
Connects mocking to OpenAPI, JSON Schema, API examples, consumer prototyping, and implementation prototypes.

Also explained in

Compare how 22 books explain mocking →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of The Design of Web APIs, Second Edition. The section links point to the parts of the book it draws on.