API Mock Data: Drafts, History and the Contract Check (Part 2)
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.
This is lesson 9.28, part 2, of Shift-Left API in Depth. Part 1 covered what a mock answers and how to make it fail. This part covers where mock data comes from and how to keep it accurate. Mocks, drafts, history and remembering are in every edition, and Free holds up to 50 mocks. Suggest with AI needs your own AI provider. The contract check runs from Discovery's Inventory, part of the API Discovery add-on that the 15-day trial includes.
Where can API mock data come from?
API mock data is what your mocks return to your tests. Typing it is slow, and once written, it can drift from the real API. On the Mocks screen, More ways to make mocks offers three other sources:
- An API's dependencies. Pick an API to see what it calls or takes values from, with the reason for each. Make draft mocks for those not mocked yet. The links are the same ones shown in the dependency graph.
- A recording. Choose a HAR file saved from your browser. Email addresses, tokens, long numbers and secret-looking values are replaced before anything is kept, and calls to the project's production environment are not used.
- A live capture, if you chose to keep example answers. The examples are erased with the capture.
Every mock made this way starts as a draft. A draft answers nothing.
What is a mock's lifecycle?
Under More on a mock's row:
- Read the draft's answers, then click Approve.
- Click Publish and switch on. Only a published mock can be switched on.
- When you no longer need it, Retire it with a reason. It stays off, keeps its history and can come back as a draft.
Can a mock remember data?
Yes. On the Remembering tab, tick Remember what this mock is sent and choose what the mock does: create, read, update, delete or list. Then a create followed by a read returns what was created. Mocks for the same resource share records, which expire after a time you set, up to a limit you set. Calls that name a scenario are answered as written and never touch this data.
How do you restore an earlier answer?
Earlier answers keeps a version for every change to how a mock answers, with a short note on what changed. Put this one back asks for confirmation and saves the current answer as a version too, so nothing is lost. Renaming a mock or switching it on or off creates no version.
How do AI-suggested answers work?
On Suggest with AI, you can describe a case, such as a product that is out of stock. Only the schema, an example built from it and your note are sent. Suggestions are checked against the schema before you see them, marked as AI and shown in full. AI results vary, so read each one. Only the ones you tick are added, as named responses, never as the default.
How do you use the mock server from anywhere?
The running server's panel shows a path-based address that any client able to reach the Engine can call, such as a test, a script or a front end. How to Use shows the call in cURL, JavaScript, Python and Node.
While the server is stopped, calls get a 503, so a test that forgot to start it fails clearly. A call to a path with no active mock gets a 404 that lists the project's mocks.
How do you keep a mock accurate?
After an approved specification change, affected mocks show May be out of date until someone confirms they have checked them. The mock's Contract tab previews what would change if it followed the new contract. Applying it updates only what you never changed, and keeps your own edits.
To run a contract check:
- Open Discovery, then Inventory, and find the API.
- Click See the evidence.
- Click Check the mocks against the contract.
Each response is shown as fitting the documented schema, different (with where, such as a price sent as text instead of a number), or not checked, with a reason. A response that could not be checked never counts as a pass, and the check changes nothing. SOAP mocks are compared with the fields their WSDL declares.
Which tool fits which test?
- One failure in one test: a named response and a header.
- A whole suite against a failing service: the environment's mock answer.
- Answers that depend on the request: conditions.
- Values that change per call: templates.
- A create followed by a read: remembering.
Sign off a release against the real API, never the mock.
Try it yourself
With a trial: import https://demo.totalshiftleft.ai/openapi/rest.json, make a draft mock from a dependency or a recording, then approve and publish it. Check it against its contract in Discovery's Inventory.
Recap
- Drafts from dependencies, recordings and captures answer only after a person publishes them.
- Every change to an answer keeps a version, and a mock can remember what it is sent.
- This API mock data generator keeps a person in control: AI suggestions are added only when ticked.
- The contract check shows whether each response still fits the spec.
Next: lesson 9.29 on test data generation with AI and contract checks. Also read about contract-checked API mocks.
Try it: With a trial, make a draft mock from an API's dependencies or from a recording, read it, approve it and publish it. If you have an AI provider, ask for suggested answers and add one you agree with. Then open Discovery, Inventory and check the mock against its contract.
Frequently asked questions
How can you generate API mock data without writing it by hand?+
In Shift-Left API, you can make draft mocks from an API's dependencies, from a HAR recording or from a live capture that kept example answers. You can also ask AI to suggest extra answers from the schema. Nothing answers until a person publishes a draft or accepts a suggestion.
How do you stop a mock from drifting away from the real API?+
Check it against its contract after every manual edit and specification change. After an approved spec change, Shift-Left API marks affected mocks as possibly out of date. The contract check compares each response with the documented schema when you ask for it.
Which editions include these mock features?+
Mocks, drafts, history and remembering are in every edition, and Free holds up to 50 mocks. Suggest with AI needs your own AI provider. The contract check runs from Discovery's Inventory, part of the API Discovery add-on that the 15-day trial includes.
Related lessons
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.
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.
A microservices testing strategy needs to know which APIs depend on which. Read a project's dependency graph three ways, tell a fact from a guess, see what is at risk when an API changes, and find one API's neighbours.
Read more on the blog
Mocks that drift from the API contract make tests pass for the wrong reasons. This guide explains how mock drift happens, how to check a mock against the contract, why "not checked" must be its own result, and how to cover error paths and failures with named mock responses.
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.