FDE Times dataFDE jobs open 911▼17 Change in open roles over 7 days, like-for-like: roles from companies we started tracking this week are left outNew in 7 days 66Companies hiring 200Remote-friendly 24%Median US pay $200kTop hirer Databricks 106
中文VI

The newspaper of the Forward Deployed Engineer

Tools

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.

ShareLinkedInFacebookX

In brief

  • WireMock was created in 2011 because Tom Akehurst's team kept having to integrate with APIs that were not finished. FDEs doing integration work on client sites run into the same situation all the time.
  • WireMock can simulate latency, intermittent 500 errors, garbage responses and reset connections, so you can test retries and timeouts before go-live.
  • Learn it in this order: basic stubs, then faults, then scenarios, and finally record and playback.
Timeline from 0 to 4 seconds for the second call after a retry. The client waits until the 2-second mark, when the timeout cuts the request. The 200 OK response only arrives at 3 seconds. The server has finished processing, but the user still sees an error.
A WireMock stub returns 200 after 3 seconds while the client waits only 2, so the request is cut even though the server has finished processing. Source: hypothetical example in the article, built with WireMock's scenarios and fixedDelayMilliseconds.

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.

{
  "scenarioName": "Flaky orders",
  "requiredScenarioState": "Started",
  "newScenarioState": "Recovered",
  "request": { "method": "GET", "url": "/api/orders/42" },
  "response": { "status": 500 }
}
{
  "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.

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.

Was this article useful?

Use with your AI assistantAsk Claude ↗Ask ChatGPT ↗
6 sources
Read next on the roadmap · Stage 5: DeploymentThe job you handed over has stopped running, and nobody knowsHealthchecks.io doesn't wait for an error: it raises the alarm when a scheduled job goes quiet past its deadline, catching the failures logs never record.