API Mock Response: Scenarios and Failures (Part 1)
A mock that only answers with success cannot show how your app handles failure. Ask for a response by name, make a mock time out or cut the connection, set answers per environment, and generate answers at the limits of a schema.
This is lesson 9.28, part 1, of Shift-Left API in Depth. It follows lesson 9.27 and builds on lesson 9.9, where you generated mocks from a spec and pointed a test at the mock server. Everything here is in every edition, and Free holds up to 50 mocks.
What should an API mock response cover?
An API mock response is what a mock server sends back when your test calls it. Real services can fail in at least four ways: they return an error status, answer slowly, never answer, or cut the connection. A mock that only ever returns success tests none of these.
How does a mock choose which response to send?
Every response has a name. The mock picks in this order:
- The response the call names, with an
X-Mock-Scenarioheader or amockScenarioquery parameter. - If the call names none, the environment's own Mock answer, if one is set and the mock has it.
- Otherwise, a response whose conditions all match the request.
- Otherwise, the default response.
If a call names a response the mock does not have, it gets a 404 listing the names that exist. It never falls back to the default.
How do you find and read your mocks?
Tick Mocks under Top menu tabs, open it and choose a project. You can search and group mocks, switch them on or off one at a time or in bulk, and edit or delete them.
A mock generated from a spec holds a default success response, plus named responses for documented errors. In REST mocks, some error answers also come back on their own when a request triggers them, such as an unknown ID. If a generated test expects an error that the spec does not document, the response is still created, and its name ends with "(not in the spec)". SOAP and GraphQL mocks hold one response.
How do you ask for a response by name?
In a test that runs against the mock, open Request, add a header called X-Mock-Scenario and set its value to the response name, such as 404. Run the test. If the client cannot send headers, add mockScenario to the address instead.
Check more than the status code. Also assert on a field that only the real error contains, so that a mistyped name cannot pass by accident.
How do you make a mock fail on purpose?
Edit the mock, click Add Response, name it (for example outage) and open How it answers:
| Option | What the caller sees |
|---|---|
| Answers normally | The status and body you set |
| Never answers | Nothing; the call is held for up to 300 seconds, so it times out |
| Cuts the connection | No status code and no body |
You can also return any status code, such as 503, or add a delay in milliseconds for a slow answer. A response behaves the same way every time, so your test is repeatable.
How do you get different answers without a header?
- Environment mock answer: in the project's environment settings, name a response under Mock answer. Calls that come through that environment's address get it, which is useful for a suite of failure tests. A header still wins, and a mock without that response answers as usual.
- Conditions: choose a response based on a field in the request.
- Dynamic templates: include variables such as a new UUID, a timestamp, a random number or a value from the request.
When you create a mock linked to an endpoint, its answers are filled in from the spec. A standalone mock uses a path and method you type.
How do you generate many answers at once?
In Generate Mock, tick Also add answers at the limits. This adds named answers for the smallest and fullest responses the schema allows, plus an empty one. They never replace the default, and an empty list is a good case to test.
If an endpoint already has a mock, you can skip it, update it from the spec or create a duplicate. Skip is the default. Updating replaces the mock's responses, including ones you added by hand.
What makes a good mock response?
- Keep the default a success.
- Name responses by what they mean, such as
outage,slowornot-found, because tests send these names. - Use a header for one test, and an environment's mock answer for a whole suite.
Try it yourself
Start free: generate a mock for one endpoint of https://demo.totalshiftleft.ai/openapi/rest.json and add an outage response that cuts the connection. In a test that runs on the mock, send X-Mock-Scenario with 404, then outage. Then generate another mock with answers at the limits.
Recap
- An API mock response is chosen by name, then by conditions, then by default.
- A mock API error response comes from the spec's documented errors and any you add.
- A mock API timeout, a delay or a cut connection tests failures beyond error codes.
- Generate Mock can add answers at the limits of a schema.
Next: part 2, mock data, history and the contract check. For background, read API mocking vs stubbing.
Try it: Generate a mock for one endpoint of the sandbox specification and add a response named outage that cuts the connection. In a test that runs against the mock, add the X-Mock-Scenario header and run it with 404, then with outage. Then generate another mock with answers at the limits.
Frequently asked questions
How do you make a mock API return an error response?+
Give the response a name and a status code, then ask for it by name. In Shift-Left API, send the X-Mock-Scenario header or the mockScenario query parameter with that name. Mocks generated from a spec include named responses for the errors the spec documents.
How do you simulate a mock API timeout?+
Set the response to never answer, so the call times out, or add a delay in milliseconds to make it slow. You can also make a response cut the connection, which behaves like a dropped service and not like an error status.
What happens if you ask a mock for a response it does not have?+
Shift-Left API returns a 404 that lists the names the mock does have. It never falls back to the default response, so a typing mistake does not quietly return a success.
Related lessons
When an auditor asks who changed a test or what was tested against which control, you need a log of what people did and files that explain themselves. Read the audit log, export evidence, and schedule reports.
Run your tests before the real service is ready, or while it's down. Generate mocks from the spec in Shift-Left API, start the mock server, and send a test to it by switching one environment.
Typing mock data by hand is slow, and it can drift from the real API. Make draft mocks from dependencies and recordings, publish them, keep their history, add AI-suggested answers you approve, and check mocks against their contract.
Read more on the blog
What API mocking is, when to mock an API and when not to, static vs dynamic mocks, how to mock with Prism, WireMock, Postman, Playwright and MSW, mocking in API testing, and how to keep mocks from drifting.
Mocking vs stubbing for APIs, plus fakes, spies and service virtualization: what each test double does, when to use which, and worked examples with WireMock, MSW and Prism.