Skip to the page

API testing · senior

Test data, stubs and contract tests

Good API tests make their own data, replace unstable third parties with stubs, and use contract tests to keep both sides in agreement.

The notes

What you must know

  • Ways to reach a state like "order Shipped", best first: the service APIs, a test-only API outside production, a stub of the other system, database seed scripts, and last fixed shared data.
  • Each test makes its own data with unique ids, so parallel runs and other teams do not interfere. Clean up through the API, tag data with a run id and purge it, or use short-lived environments.
  • Use builders in code (OrderBuilder.shipped().withItems(2).create()). Use synthetic personal data, not copies of production.
  • When a third-party sandbox is unstable, run most tests against stubs (WireMock, MockServer, Mountebank) and keep a small separate job for the real sandbox.
  • Stubs should return realistic responses and also failures: a timeout, a 500, broken JSON, a webhook delivered twice. Check for drift with contract tests or a comparison with the provider spec.
  • In Playwright, the request fixture can create data through the API before a UI check. page.request shares cookies with the browser context.
  • Pact consumer test: use matchers (stringType, integerType) not exact values, include only the fields the consumer reads, and name the provider state exactly. The provider verifies and can-i-deploy checks versions.
  • Pact is not an end-to-end test and does not hold business rules.

The red pen

Common traps: mistake → fix

  • Mistake: Drive every precondition through the UI

    Fix: it is slow and flaky; use APIs.

  • Mistake: Mock everything

    Fix: nothing checks the real integration; keep a small real job.

  • Mistake: A Pact contract should list every field

    Fix: it blocks the provider from changing fields nobody uses.

Easy to hard

Questions interviewers ask, easy to hard

  1. How do you get an order into the Shipped state for a UI test?
  2. The payment sandbox is down half the time. What is your plan?
  3. What is a consumer-driven contract test?
  4. Why should a Pact test use matchers?

Explain it out loud

Explain it out loud to an AI interviewer that follows up, and see where your answer is thin.

Practise by voice

Your first interview is free

Sign up and start with a 15-minute interview with the full report, and an English conversation. No card needed.

Start your free interview