Expectation structure
An expectation contains:httpRequest— the request matcher that identifies which incoming requests this expectation applies to- action — one of
httpResponse,httpForward,httpOverrideForwardedRequest,httpResponseTemplate,httpForwardTemplate,httpResponseClassCallback, orhttpError(see Response Actions) times(optional) — how many times the expectation should fire before it is deactivatedtimeToLive(optional) — how long the expectation remains activepriority(optional) — matching order relative to other expectations; higher values match firstid(optional) — a stable identifier used to update an existing expectation
Complete example
Field reference
times
times
Controls how many times an expectation matches before it is automatically deactivated.Set
"unlimited": true to keep the expectation active indefinitely (the default when times is omitted).In Java, use Times.exactly(2), Times.once(), or Times.unlimited().When you register multiple expectations with the same request matcher and different times values, MockServer fires them in registration order. For example, if expectation A fires three times then expectation B fires twice, requests are handled A, A, A, B, B.timeToLive
timeToLive
Sets an expiry duration after which the expectation is automatically removed, regardless of how many remaining times it has.In Java, use
TimeToLive.exactly(TimeUnit.SECONDS, 60L).priority
priority
An integer that controls matching order. MockServer evaluates expectations from highest priority first, then by creation order for equal-priority expectations.Use a negative priority to create a catch-all default expectation that only matches after everything else has been tried:In Java, pass priority as the fourth argument to
.when():id
id
A string identifier for the expectation. If you submit an expectation with an This is useful for updating a specific expectation without clearing all others.
id that matches an existing expectation, the existing expectation is replaced entirely. MockServer assigns a UUID automatically when no id is provided.Matching order
MockServer matches active expectations in priority order, then creation order. Once a matching expectation’stimes counter reaches zero, it is deactivated. If no active expectation matches an incoming request and MockServer is not acting as a proxy, it returns a 404.
Next steps
Request Matchers
Learn how to match by method, path, headers, body, and more.
Response Actions
Return responses, forward requests, invoke callbacks, or simulate errors.