# Break the client's mock API with WireMock before go-live

> Making a mock API stall, throw errors and drop connections on your own machine exposes badly set timeouts and retries before go-live, while they can still be fixed.

Original: https://fdetimes.net/en/tools/wiremock-fault-testing-before-go-live/

In 2011 Tom Akehurst wrote WireMock as a Java library. He told Tech Lead Journal that his team kept being asked to integrate with an API before that API was finished. WireMock's official site now reports more than 5 million downloads a month.

FDEs run into the "API not ready yet" problem Akehurst describes all the time. The client's system may still be waiting for IT to open a firewall. The staging environment may be unstable, and a partner may not have issued a sandbox yet.

That leaves two options: wait, or stand up a fake API that behaves like the real one. WireMock's documentation states plainly that the tool exists to isolate you from flaky third parties and to simulate APIs that do not exist yet. It is licensed under Apache-2.0, so it can be used free of charge on client projects.

## Anyone can test the happy path

Every team can build a mock that returns a 200 with a tidy JSON body. WireMock earns its keep when it simulates failure.

Akehurst calls the approach over-the-wire mocking: mocks that run over real HTTP. In his view, it brings learning and feedback into your everyday coding loop. You find out how your code handles failure while you are writing it, rather than waiting for staging to break.

Take a hypothetical case. Your agent calls `GET /api/orders/42` on the client's order system. That system sometimes returns a 500, and when it does work it takes several seconds. WireMock scenarios are state machines, so you can write two chained stubs: the first call fails, the next succeeds but slowly.

```json
{
  "scenarioName": "Flaky orders",
  "requiredScenarioState": "Started",
  "newScenarioState": "Recovered",
  "request": { "method": "GET", "url": "/api/orders/42" },
  "response": { "status": 500 }
}
```

```json
{
  "scenarioName": "Flaky orders",
  "requiredScenarioState": "Recovered",
  "request": { "method": "GET", "url": "/api/orders/42" },
  "response": {
    "status": 200,
    "fixedDelayMilliseconds": 3000,
    "jsonBody": { "id": 42, "status": "shipped" }
  }
}
```

Now run the numbers. Suppose the client sets a 2-second timeout and retries once. The first call gets a 500, so the client retries. The second call is delayed by 3 seconds, past the 2-second timeout, so the request is cut off even though the server finished processing it.

The user sees an error and the order is never read. With two JSON files, you learn that a 2-second timeout is too short, and you learn it before go-live.

## A 500 is the easiest failure

A 500 has one virtue: the client notices it immediately. Some failures are more dangerous because they look like success, and WireMock has a separate set of faults for exactly those cases.

| Simulation | What happens | What it lets you test |
|---|---|---|
| Fixed delay | The response arrives only after the set number of milliseconds | Is the timeout in the right place? |
| Random sampled delay | Latency fluctuates like real traffic | Can the system cope when a few requests are unusually slow? |
| Chunked dribble delay | Data trickles in as on a weak network | Does the timeout behave the same on every run? |
| MALFORMED_RESPONSE_CHUNK | Sends a 200, then garbage data, then closes the connection | Does the code trust the 200 without checking the body? |
| CONNECTION_RESET_BY_PEER | The connection is reset abruptly | Do retries catch socket-level errors? |

The MALFORMED_RESPONSE_CHUNK row deserves the most attention. If integration code checks only the status code and then parses straight away, a 200 with a garbage body will either crash it or, worse, write corrupt data into the client's system.

**Key point:** A response carrying a 200 can still be a failure.

## A mock knows only what you teach it

Mocks have one big limitation: they are only as accurate as the assumptions of whoever wrote them. If you hand-write JSON from memory of an old API document, you are testing your code against an API that does not exist.

Record and playback narrows that gap. Combined with its proxy feature, WireMock records stub mappings from real calls to the client's running API. Route traffic through the proxy for a session and you have a set of stubs with the real response structure, on top of which you add your own faults and delays.

A recorded set of stubs is still a snapshot. If the client changes the schema next week, your mocks stay green while production goes red. So re-record the stubs whenever the client reports a change, and keep a few tests that hit the real API directly before each release.

## In what order should you learn it?

Start with basic stubs: match the method and URL, return a status and a body. Then learn faults, because this is what separates people who can only build demos from people who can take a system to production.

Third come scenarios, for when you need to simulate behavior that changes from one call to the next. Record and playback comes last; you will need it on your first day inside the client's internal network.

For your career, writing "used WireMock" on a CV is not enough. Be specific about the failures you simulated and what you found, for example: simulated connection resets and malformed responses, and caught a misconfigured timeout before go-live.

Let the client's API break on your machine first. On go-live day, there will be fewer surprises.

**Try this week:**

- Pick a third-party API your code calls, write two scenario stubs (500, then 200) and check whether your retry actually fires
- Add a stub that returns MALFORMED_RESPONSE_CHUNK and see how your client handles a 200 with a broken body
- Add one specific line to your CV or project README: which failures were simulated and tested before go-live

## Sources

- [WireMock Java - API Mocking for Java and JVM | WireMock](https://wiremock.org/docs/)

- [wiremock/wiremock (GitHub)](https://github.com/wiremock/wiremock)

- [Simulating Faults | WireMock](https://wiremock.org/docs/simulating-faults/)

- [Stateful Behaviour | WireMock](https://wiremock.org/docs/stateful-behaviour/)

- [Record and Playback | WireMock](https://wiremock.org/docs/record-playback/)

- [#210 - Creator of WireMock: Building a Successful Open Source Project and The Art of API Mocking - Tom Akehurst](https://techleadjournal.dev/episodes/210/)
