skip to content

In MockServer, how would you choose between a document's declared examples and withGenerateFromSchema() across many teams' beehive telemetry mocks?

level: principalimportance: should knowfreq 45%

answer

  1. two body sources, one document
  2. an example was written by a person
  3. a schema promises shape, not meaning
  4. MockServer withGenerateFromSchema on HttpResponse
  5. assert on examples, never on generated values

basics

~20 s

Prefer the document's declared examples: MockServer returns a body a person wrote and a reviewer saw. Use withGenerateFromSchema on MockServer's HttpResponse where only a schema exists. Treat generated values as structurally valid and semantically meaningless, and never assert on them.

solid answer

~50 s

Two bodies can come out of the same beehive telemetry document, and they are not interchangeable. Where an operation declares an example, MockServer replies with a body somebody wrote deliberately: `weightKg` sits in a plausible range, `queenPresent` is a value the domain actually produces, and a test can assert on it. Where the operation declares only a schema, the body is synthesised — MockServer's `HttpResponse.withGenerateFromSchema(` does this explicitly — and it guarantees exactly what a schema guarantees: right types, required keys present, declared bounds respected, and nothing else. Across many teams the rule that scales is to make examples the reviewed artefact for the operations tests assert on, and let schema generation cover the long tail where no assertion depends on the values. The failure to design against is a suite whose assertions quietly rest on generated values, because those change the moment a schema does.

code

java · 16 lines
java
import java.nio.file.Files;
import java.nio.file.Path;

import static org.mockserver.model.HttpRequest.request;
import static org.mockserver.model.HttpResponse.response;

// the JSON schema copied out of the beehive telemetry document
String hiveWeightSchema = Files.readString(Path.of("schemas/hive-weight.json"));

client.when(
    request().withMethod("GET").withPath("/hives/h-0417/weight")
).respond(
    response()
        .withStatusCode(200)
        .withGenerateFromSchema(hiveWeightSchema)
);

go deeper

for a junior

Know that a mock built from an OpenAPI document can reply either with an example the document declares or with a body MockServer generates from the schema, and that those two are not the same thing.

for a middle

Explain what MockServer's withGenerateFromSchema actually guarantees: declared types, required keys and declared bounds. Nothing about whether the values make sense for a beehive.

for a senior

Be ready to spot a suite that asserts on synthesised values, say why it will break on the next schema edit, and move those assertions onto declared examples instead.

for a principal

Own the policy across teams: decide which beehive telemetry operations must carry reviewed examples, who reviews a change to one, and where schema generation is accepted as good-enough coverage.

## Two bodies, one document A specification-derived mock has two possible sources for the body it returns, and confusing them is where fidelity quietly leaks. The beehive telemetry document declares, for each response of each operation, either a concrete **example** or just a **schema** — often both, often neither on the error paths. MockServer will happily serve either, and the two are not the same kind of artefact at all. An example is a **decision**. Somebody wrote `weightKg` as a value a real hive on a real pallet would report, made `queenPresent` consistent with the `broodTempC` in the same object, and put `hiveId` in the format the registry actually issues. A reviewer saw it. It changes only when a person changes it. A schema-generated body is a **derivation**. MockServer's `HttpResponse.withGenerateFromSchema(` takes a JSON schema and produces something that satisfies it. That is a real guarantee and a narrow one. ## What generation actually guarantees - Declared **types** hold: a numeric `weightKg` comes back numeric, a string `capturedAt` comes back a string. - **Required** keys are present, so a client that reads them without a null check does not fall over. - Declared **bounds and formats** are respected where the schema states them. - Nothing else. There is no guarantee that `weightKg` is plausible for a beehive, that `varroaCount` is consistent with `beeCountEstimate`, or that two calls agree with each other. That last point is the one that costs teams real time. A generated body is structurally correct and semantically arbitrary, so an assertion written against it is an assertion against an accident. ## The choice, stated as a rule Across a dozen teams sharing one beehive telemetry document, the version of this that survives contact is not "examples good, generation bad". It is a split by **what the test does with the body**: 1. If a test **asserts on field values**, the operation must carry a declared example, and that example is a reviewed artefact — changed on purpose, in a pull request somebody reads. 2. If a test only needs the call to **succeed with the right shape**, schema generation is fine and cheaper, and asking every team to hand-author an example for it is bureaucracy with no reader. 3. If a test drives an **error path**, check what the document actually declares. Error responses are the thinnest part of most documents, so the honest answer is often that the example needs writing before the test can be meaningful. ## Who owns what, once it is a policy The decision stops being a per-test preference the moment more than one team consumes the same document. Concretely, somebody has to own: - **Which operations must carry examples.** A short list beats a blanket rule; the list is the operations that other teams' assertions actually touch. - **Who reviews an example change.** An example is now behaviour for every downstream mock, so an edit to it is not a documentation tweak. - **What happens when a schema changes under a generated body.** Generated values shift, and any suite that leaned on them fails — usefully, if people expected it; mysteriously, if they did not. - **Where generation is explicitly declared acceptable**, so nobody re-litigates it in each review. ## The failure mode to design against The damaging pattern is not a wrong body. It is a **green suite built on a generated one**. A test asserts that the beehive telemetry response carries a `weightKg` above zero, that passes because the generator emitted a positive number, and the team reads it as evidence the client handles weights correctly. Nothing about that assertion was ever anchored to a decision anyone made. Then a schema edit changes the generated value, the assertion breaks, and the fix that looks obvious — loosen the assertion — removes the last trace of intent from the test. The attribution matters when you write this down: `withGenerateFromSchema(` is MockServer's, on `HttpResponse`, sitting alongside `withStatusCode(`, `withBodyFromFile(` and `withReasonPhrase(`. **WireMock's response builder is a different one, where the status setter is `aResponse().withStatus(`** and the body-from-file setter is spelled `withBodyFile(` rather than MockServer's `withBodyFromFile(`. On a subject where three servers share one vocabulary, saying which product a method belongs to is part of the answer, not decoration around it. ## The short version Examples are evidence of intent; generated bodies are evidence of shape. Point your assertions at the first, let the second carry coverage, and make sure the list of operations that must carry examples is written down somewhere a person owns rather than living in each team's memory of a conversation.

  • What does MockServer's HttpResponse.withGenerateFromSchema guarantee about the body it produces?
    Only what the schema states: the declared type of each field, the presence of required keys, and any declared bounds or formats. It says nothing about whether a generated `weightKg` is plausible for a hive, nothing about consistency between fields, and nothing about stability — change the schema and the values may change with it.
  • Why is a declared example the safer thing for a test to assert on?
    Because a person chose it and a reviewer saw it, so it carries domain meaning a schema cannot express: a `queenPresent` flag consistent with a `broodTempC` reading, an identifier in the format the real registry issues. MockServer returns it as written, so an assertion on it fails only when somebody deliberately changes the document.

A schema-generated body is a show home furnished from the floor plan: every room is where the drawing says, but the books on the shelf are props. A declared example is a photograph of a house somebody actually lived in.

saying these in an interview costs you the question

  • Treats a schema-generated value as domain-realistic
  • Asserts on fields MockServer synthesised from a schema
  • Says MockServer's withGenerateFromSchema belongs to another product
  • Assumes generated bodies stay stable across schema edits
  • Believes examples and schema generation are interchangeable