When should a WireMock stub's water-rota body move from withBody() to withBodyFile()?
answer
- the mapping keeps the matcher, not the payload
- the file name lives in the response definition
- resolved under the __files directory
- reuse, tooling and size are the triggers
- WireMock withBodyFile, MockServer withBodyFromFile
basics
~20 sMove it once the payload buries the matcher, when several mappings need the same fixture, or when the bytes want to be a real file. WireMock's withBodyFile names a file under __files, leaving the mapping with the matcher and status.
solid answer
~50 sIn WireMock, `withBody(String)` stores the payload inside the mapping, while `withBodyFile("water-rota/plot-17-slots.json")` stores a file name and WireMock reads the body from that file under `__files`. I move a body out once the mapping stops being readable — a long allotment water-rota slot list drowns the request pattern that actually matters — or when several mappings should answer with the same fixture, or when the payload deserves to be a real `.json` file that an editor formats and a diff shows line by line. What changes is only the response definition; the request pattern is untouched. The costs are real: the stub now depends on a file being present wherever that server loads files from, and a reviewer needs two files open instead of one. MockServer expresses the same idea as `withBodyFromFile(` on its `response()`, which is a different spelling of the same decision.
code
java · 5 linesstubFor(get(urlPathEqualTo("/allotment/rota/plots/17/slots"))
.willReturn(aResponse()
.withStatus(200)
.withHeader("Content-Type", "application/json")
.withBodyFile("water-rota/plot-17-slots.json")));go deeper
Know that a WireMock stub's body can be a literal string via withBody or a file name via withBodyFile, and that the file name is resolved under __files rather than being served as text itself.
Explain what moves and what does not: only the response definition changes, the request pattern is untouched, and the mapping picks up a dependency on the fixture file being present.
Give a threshold, not a taste. Say when a payload has outgrown its mapping, what a shared fixture couples together, and how you name files so a wrong reply is traceable from the mapping.
Set the house rule across suites: which bodies live inline, how fixtures are named and packaged, and how the team stops one shared file from silently coupling unrelated stubs together.
## What withBodyFile actually changes A WireMock stub's response definition can hold its payload in one of two places. WireMock's `withBody(String)` puts the bytes in the mapping itself, so the stub is one self-contained object. WireMock's `withBodyFile(` puts a **file name** in the mapping instead, and WireMock resolves that name under the `__files` directory of the file root the server is running with, serving the file's contents as the body. The substitution is narrow and worth stating precisely: - Only the response definition changes. The request pattern — the method, the URL matcher, any header or body matchers — is untouched. - The status and headers stay in the mapping. `withBodyFile(` replaces the body call, not `withStatus(` or `withHeader(`. - The mapping gains a dependency. It is now correct only if a file of that name exists where that server looks for files. Which directory that is depends on how the server was started, which is a separate subject from this decision. - Nothing about matching changes. The same requests select the same stub; only what comes back is sourced differently. ## When the payload has outgrown the mapping The usual trigger is readability. A water-rota stub for `GET /allotment/rota/plots/17/slots` whose body is a forty-entry slot list turns the mapping into a wall of JSON with the interesting part — the matcher — buried at the top. The reason to move it is that a reviewer should be able to see what the stub answers *for* without scrolling past what it answers *with*. The other honest triggers: - **Reuse.** Several mappings answer with the same rota payload, and you would rather have one file than four copies that drift apart. - **Tooling.** A real `.json` file can be formatted, linted, validated against a schema and diffed line by line. The same bytes embedded as a string inside a mapping get none of that. - **Size.** Past a certain payload size the mapping is no longer a document anybody reads; it is a container. - **Non-text bytes.** A binary export cannot be a string at all, so a file is one of the two ways to serve it. ## When it should stay inline Moving early is a real mistake, not a safe default: - A short body — an error envelope, a two-field acknowledgement, an empty list — is clearest where you can see it beside the status it accompanies. - A stub whose whole point is the payload, such as one that reproduces a malformed allotment response, is easier to trust when the malformed bytes are visible in the mapping. - A stub that must travel as a single self-contained object cannot depend on a companion file being shipped with it. The rule of thumb that survives review: **inline while the body is evidence, filed once the body is data.** ## The cross-product spelling WireMock's HTTP response builder spells the file-backed body `withBodyFile(`, and that is the only name for it on `aResponse()`. In MockServer the same idea is spelled `withBodyFromFile(`. The two are one keyword apart, belong to different products and generate confident wrong code in both directions, because each name is genuinely real — just not on the builder somebody reached for. The discipline that removes the risk costs nothing: say the product's name in the same sentence as the setter, every single time. ## The costs you are taking on 1. **A second place to look.** Diagnosing a wrong reply now means opening the mapping *and* the fixture. Name files so the mapping tells you which one, for example `water-rota/plot-17-slots.json` rather than `response3.json`. 2. **A packaging dependency.** The stub is only correct if the fixture ships with it. A suite that passes locally and fails elsewhere because the file did not travel is the standard failure of this mechanism. 3. **Weaker locality of review.** A change to the fixture no longer shows up as a change to the stub. That is a gain for diffs of the payload and a loss for anyone reviewing behaviour. 4. **Duplication moves rather than disappears.** One fixture shared by four stubs makes all four change together, which is right when they model the same response and wrong when they only happened to look alike. ## What interviewers listen for - That you can state the mechanism exactly: WireMock's `withBodyFile(` takes a file name, resolved under `__files`, and serves that file's contents as the body. - That you name a threshold rather than a preference — readability, reuse, tooling, non-text bytes — instead of "files are cleaner". - That you know the request side is unaffected, so this is never a fix for a stub that does not match. - That you keep the spelling attributed: `withBodyFile(` is WireMock's, `withBodyFromFile(` is MockServer's, and blending them is the defect this whole subject area is prone to. A good answer ends with the trade-off rather than the mechanic: moving a body to a file buys a readable mapping and costs a second artefact that must stay with it.
- Does moving a WireMock body into withBodyFile() change which requests the stub matches?No. `withBodyFile(` sits on the response definition, so the method, the URL matcher and any header or body matchers are untouched. A stub that was never selected before is still never selected. Reaching for a file body to fix a non-matching stub is a category error: the diagnosis belongs on the request side, not the reply side.
- What breaks when a WireMock mapping references a body file that is not there?The mapping is no longer self-contained, so it is only correct where its fixture exists. The usual symptom is a suite that passes on the machine where the file was written and fails wherever the stub set was copied without it. Treat the fixture as part of the stub and ship them together.
- How do you keep a shared body file from coupling stubs that only look alike?Share a fixture when the stubs genuinely model the same response, and copy it when they merely resemble each other today. A shared file makes all its consumers change at once, which is right for one payload and wrong for four independent ones that happened to start identical.
saying these in an interview costs you the question
- Writing withBodyFromFile on WireMock's aResponse() builder
- Thinking withBodyFile changes which requests match
- Assuming the fixture file travels automatically with the mapping
- Moving every body to a file regardless of size
- Believing withBodyFile renders the file per request