intermediate·10 min read·Updated

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:

  1. Read the draft's answers, then click Approve.
  2. Click Publish and switch on. Only a published mock can be switched on.
  3. 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:

  1. Open Discovery, then Inventory, and find the API.
  2. Click See the evidence.
  3. 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.

Hands-on lab

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.

Enter your email to open the lab steps. One unlock opens every lab in the learning center.

No spam. Protected by reCAPTCHA — Google Privacy & Terms.

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.

Read more on the blog