- Author:
- jamesdbloom
-
Method Summary
Modifier and TypeMethodDescriptionorg.mockserver.mock.Expectation[]error(org.mockserver.model.HttpError httpError) Return error when expectation is matchedorg.mockserver.mock.Expectation[]forward(org.mockserver.mock.action.ExpectationForwardCallback expectationForwardCallback) Call method on object locally or remotely (over web socket) to generate request to forward when expectation is matchedorg.mockserver.mock.Expectation[]forward(org.mockserver.mock.action.ExpectationForwardCallback expectationForwardCallback, org.mockserver.mock.action.ExpectationForwardAndResponseCallback expectationForwardResponseCallback) Call method on object locally or remotely (over web socket) to generate request to forward when expectation is matchedorg.mockserver.mock.Expectation[]forward(org.mockserver.mock.action.ExpectationForwardCallback expectationForwardCallback, org.mockserver.mock.action.ExpectationForwardAndResponseCallback expectationForwardResponseCallback, org.mockserver.model.Delay delay) Call method on object locally or remotely (over web socket) to generate request to forward when expectation is matchedorg.mockserver.mock.Expectation[]forward(org.mockserver.mock.action.ExpectationForwardCallback expectationForwardCallback, org.mockserver.model.Delay delay) Call method on object locally or remotely (over web socket) to generate request to forward when expectation is matchedorg.mockserver.mock.Expectation[]forward(org.mockserver.model.HttpClassCallback httpClassCallback) Call method on local class in same JVM implementing ExpectationResponseCallback to generate request to forward when expectation is matchedorg.mockserver.mock.Expectation[]forward(org.mockserver.model.HttpForward httpForward) Forward request to the specified host and port when expectation is matchedorg.mockserver.mock.Expectation[]forward(org.mockserver.model.HttpOverrideForwardedRequest httpOverrideForwardedRequest) Override fields, headers, and cookies etc in request being forwarded with specified fields, headers and cookies, etc in the specified request when expectation is matchedorg.mockserver.mock.Expectation[]forward(org.mockserver.model.HttpTemplate httpTemplate) Evaluate Velocity or JavaScript template to generate request to forward when expectation is matchedorg.mockserver.mock.Expectation[]forwardWithFallback(org.mockserver.model.HttpForwardWithFallback httpForwardWithFallback) Forward request to the upstream host and return a fallback response when the upstream returns a configured status code (default 5xx) or times out.org.mockserver.mock.Expectation[]Set a list of responses to cycle through sequentially when expectation is matched.org.mockserver.mock.Expectation[]respond(org.mockserver.mock.action.ExpectationResponseCallback expectationResponseCallback) Call method on object locally or remotely (over web socket) to generate response to return when expectation is matchedorg.mockserver.mock.Expectation[]respond(org.mockserver.mock.action.ExpectationResponseCallback expectationResponseCallback, org.mockserver.model.Delay delay) Call method on object locally or remotely (over web socket) to generate response to return when expectation is matchedorg.mockserver.mock.Expectation[]respond(org.mockserver.model.HttpClassCallback httpClassCallback) Call method on local class in same JVM implementing ExpectationResponseCallback to generate response to return when expectation is matchedorg.mockserver.mock.Expectation[]respond(org.mockserver.model.HttpResponse httpResponse) Return response when expectation is matchedorg.mockserver.mock.Expectation[]respond(org.mockserver.model.HttpTemplate httpTemplate) Evaluate Velocity or JavaScript template to generate response to return when expectation is matchedorg.mockserver.mock.Expectation[]respondWithBinary(org.mockserver.model.BinaryResponse binaryResponse) org.mockserver.mock.Expectation[]respondWithDns(org.mockserver.model.DnsResponse dnsResponse) org.mockserver.mock.Expectation[]respondWithGrpcBidi(org.mockserver.model.GrpcBidiResponse grpcBidiResponse) org.mockserver.mock.Expectation[]respondWithGrpcStream(org.mockserver.model.GrpcStreamResponse grpcStreamResponse) org.mockserver.mock.Expectation[]respondWithLlm(org.mockserver.model.HttpLlmResponse httpLlmResponse) org.mockserver.mock.Expectation[]respondWithSse(org.mockserver.model.HttpSseResponse httpSseResponse) org.mockserver.mock.Expectation[]respondWithWebSocket(org.mockserver.model.HttpWebSocketResponse httpWebSocketResponse) org.mockserver.mock.Expectation[]upsert()Submit the expectation to MockServer without setting any additional primary action.withAfterAction(org.mockserver.model.AfterAction afterAction) Append an after-action to execute after the primary action completes, after any after-actions already set, so chained calls register every action in order; to replace the whole list usewithAfterActions(AfterAction...).withAfterActions(org.mockserver.model.AfterAction... afterActions) Replace this expectation's after-actions with the given list, which run after the primary action completes; an empty or null argument leaves the current list unchanged.withBeforeAction(org.mockserver.model.AfterAction beforeAction) Append a before-action to execute before the primary action, after any before-actions already set, so chained calls register every action in order; to replace the whole list usewithBeforeActions(AfterAction...).withBeforeActions(org.mockserver.model.AfterAction... beforeActions) Replace this expectation's before-actions with the given list, which run in order before the primary action; an empty or null argument leaves the current list unchanged.withCapture(List<org.mockserver.model.CaptureRule> capture) Set the capture rules that extract values from the matched request into scenario state, so later template responses can read them via thescenariohelper.withCapture(org.mockserver.model.CaptureRule... capture) Set the capture rules that extract values from the matched request into scenario state, so later template responses can read them via thescenariohelper.withChaos(org.mockserver.model.HttpChaosProfile chaos) Set a declarative HTTP chaos/fault injection profile for this expectation.withCrossProtocolScenario(org.mockserver.model.CrossProtocolScenario crossProtocolScenario) Add a single cross-protocol scenario correlation to this expectation.withCrossProtocolScenarios(List<org.mockserver.model.CrossProtocolScenario> crossProtocolScenarios) Set the list of cross-protocol scenario correlations for this expectation.withHttpResponses(List<org.mockserver.model.HttpResponse> httpResponses) Set a list of responses for stateful / multi-response selection.Set id of this expectation which can be used to update this expectation later or for clearing or verifying by expectation id.withMetadata(String key, Object value) Add (or replace) one metadata entry on this expectation, for example an owner or a ticket number.withMetadata(Map<String, ?> metadata) Set all metadata on this expectation, replacing any already set; null or empty removes it.withMetadataJson(String key, String jsonValue) Add (or replace) one metadata entry whose value is given as JSON text, for examplewithMetadataJson("tags", "[\"smoke\",\"checkout\"]").withNamespace(String namespace) Set the optional namespace (a.k.a. tenant) that this expectation belongs to, enabling multiple teams or test-suites to share a single MockServer instance without their expectations colliding.withNewScenarioState(String newScenarioState) withPercentage(Integer percentage) Set percentage of requests this expectation should match (0-100).withPriority(int priority) Set priority of this expectation which is used to determine the matching order of expectations when a request is received.withResponseMode(org.mockserver.mock.ResponseMode responseMode) withResponseWeights(List<Integer> responseWeights) Set the relative weight of each response inhttpResponses(index-aligned), used whenResponseMode.WEIGHTEDselection is active.withScenarioName(String scenarioName) Set the scenario name for stateful expectation matchingwithScenarioState(String scenarioState) Set an ordered list of steps for this expectation.withSteps(org.mockserver.model.ExpectationStep... steps) Set an ordered list of steps for this expectation.withSwitchAfter(Integer switchAfter) Set the number of matches served by each response before advancing to the next, used whenResponseMode.SWITCHselection is active (default 1).
-
Method Details
-
withId
Set id of this expectation which can be used to update this expectation later or for clearing or verifying by expectation id.
Note: Each unique expectation must have a unique id otherwise this expectation will update a existing expectation with the same id.
- Parameters:
id- unique string for expectation's id
-
withPriority
Set priority of this expectation which is used to determine the matching order of expectations when a request is received.
Matching is ordered by priority (highest first) then creation (earliest first).
- Parameters:
priority- expectation's priority
-
withPercentage
Set percentage of requests this expectation should match (0-100).
When set, the expectation will only match the specified percentage of requests that structurally match, enabling probabilistic matching.
- Parameters:
percentage- percentage of requests to match (0-100), or null for all requests
-
withChaos
Set a declarative HTTP chaos/fault injection profile for this expectation.
When set, probabilistic error status injection and/or latency injection is applied to the response returned by the expectation's action.
- Parameters:
chaos- the chaos profile to apply, or null to disable chaos injection
-
withScenarioName
Set the scenario name for stateful expectation matching- Parameters:
scenarioName- the name of the scenario
-
withScenarioState
-
withNewScenarioState
-
withNamespace
Set the optional namespace (a.k.a. tenant) that this expectation belongs to, enabling multiple teams or test-suites to share a single MockServer instance without their expectations colliding.A
null, empty or whitespace-only namespace (the default) places the expectation in the global namespace, which is matched regardless of any request namespace. A non-blank namespace scopes the expectation so it only matches requests that carry the configured namespace header with this exact value.- Parameters:
namespace- the namespace (tenant) for this expectation, or null for global
-
withMetadata
Add (or replace) one metadata entry on this expectation, for example an owner or a ticket number. Metadata is returned on retrieve and can be used to retrieve or clear expectations, but never affects matching.- Parameters:
key- the metadata key, must not be blankvalue- any JSON value: a String, Number, Boolean, List, Map or JsonNode
-
withMetadataJson
Add (or replace) one metadata entry whose value is given as JSON text, for examplewithMetadataJson("tags", "[\"smoke\",\"checkout\"]").- Parameters:
key- the metadata key, must not be blankjsonValue- any JSON value as text- Throws:
IllegalArgumentException- ifjsonValueis not valid JSON
-
withMetadata
Set all metadata on this expectation, replacing any already set; null or empty removes it.- Parameters:
metadata- the metadata, string keys to any JSON value
-
withCapture
Set the capture rules that extract values from the matched request into scenario state, so later template responses can read them via thescenariohelper.- Parameters:
capture- the capture rules to apply on match
-
withCapture
Set the capture rules that extract values from the matched request into scenario state, so later template responses can read them via thescenariohelper.- Parameters:
capture- the capture rules to apply on match
-
withResponseMode
-
withHttpResponses
public ForwardChainExpectation withHttpResponses(List<org.mockserver.model.HttpResponse> httpResponses) Set a list of responses for stateful / multi-response selection. The list takes priority over a singular response and is selected from according to the expectation'sResponseMode(sequential by default).- Parameters:
httpResponses- the list of responses to select from
-
withResponseWeights
Set the relative weight of each response inhttpResponses(index-aligned), used whenResponseMode.WEIGHTEDselection is active.- Parameters:
responseWeights- the index-aligned relative weights
-
withSwitchAfter
Set the number of matches served by each response before advancing to the next, used whenResponseMode.SWITCHselection is active (default 1).- Parameters:
switchAfter- the number of matches per response block (must be >= 1)
-
withCrossProtocolScenario
public ForwardChainExpectation withCrossProtocolScenario(org.mockserver.model.CrossProtocolScenario crossProtocolScenario) Add a single cross-protocol scenario correlation to this expectation.- Parameters:
crossProtocolScenario- the cross-protocol scenario to add
-
withCrossProtocolScenarios
public ForwardChainExpectation withCrossProtocolScenarios(List<org.mockserver.model.CrossProtocolScenario> crossProtocolScenarios) Set the list of cross-protocol scenario correlations for this expectation.- Parameters:
crossProtocolScenarios- the cross-protocol scenarios
-
withBeforeActions
Replace this expectation's before-actions with the given list, which run in order before the primary action; an empty or null argument leaves the current list unchanged. To add one action to those already set usewithBeforeAction(AfterAction).- Parameters:
beforeActions- the complete list of before-actions
-
withBeforeAction
Append a before-action to execute before the primary action, after any before-actions already set, so chained calls register every action in order; to replace the whole list usewithBeforeActions(AfterAction...). Blocking before-actions (the default) can gate the response via their failure policy and timeout.- Parameters:
beforeAction- the before-action to append
-
withAfterActions
Replace this expectation's after-actions with the given list, which run after the primary action completes; an empty or null argument leaves the current list unchanged. To add one action to those already set usewithAfterAction(AfterAction).- Parameters:
afterActions- the complete list of after-actions
-
withSteps
Set an ordered list of steps for this expectation. Steps provide a unified way to declare an ordered pipeline of side-effects and a single designated responder. Exactly one step must haveresponder = true. Steps that precede the responder run before the response; steps that follow run after.When steps are set, they supersede
beforeActions+ the primary response action for dispatch ordering. The primary action is still determined by the responder step's action type.- Parameters:
steps- the ordered steps
-
withSteps
Set an ordered list of steps for this expectation.- Parameters:
steps- the ordered steps- See Also:
-
withAfterAction
Append an after-action to execute after the primary action completes, after any after-actions already set, so chained calls register every action in order; to replace the whole list usewithAfterActions(AfterAction...).- Parameters:
afterAction- the after-action to append
-
respond
public org.mockserver.mock.Expectation[] respond(org.mockserver.model.HttpResponse httpResponse) Return response when expectation is matched- Parameters:
httpResponse- response to return- Returns:
- added or updated expectations
-
respond
public org.mockserver.mock.Expectation[] respond(List<org.mockserver.model.HttpResponse> httpResponses) Set a list of responses to cycle through sequentially when expectation is matched. Each match returns the next response in the list, cycling back to the first after the last.- Parameters:
httpResponses- list of responses to cycle through- Returns:
- added or updated expectations
-
respond
public org.mockserver.mock.Expectation[] respond(org.mockserver.model.HttpTemplate httpTemplate) Evaluate Velocity or JavaScript template to generate response to return when expectation is matched- Parameters:
httpTemplate- Velocity or JavaScript template used to generate response- Returns:
- added or updated expectations
-
respond
public org.mockserver.mock.Expectation[] respond(org.mockserver.model.HttpClassCallback httpClassCallback) Call method on local class in same JVM implementing ExpectationResponseCallback to generate response to return when expectation is matchedThe callback class must: - implement org.mockserver.mock.action.ExpectationResponseCallback - have a zero argument constructor - be available in the classpath of the MockServer
- Parameters:
httpClassCallback- class to callback as a fully qualified class name, i.e. "com.foo.MyExpectationResponseCallback"- Returns:
- added or updated expectations
-
respond
public org.mockserver.mock.Expectation[] respond(org.mockserver.mock.action.ExpectationResponseCallback expectationResponseCallback) Call method on object locally or remotely (over web socket) to generate response to return when expectation is matched- Parameters:
expectationResponseCallback- object to call locally or remotely to generate response- Returns:
- added or updated expectations
-
respond
public org.mockserver.mock.Expectation[] respond(org.mockserver.mock.action.ExpectationResponseCallback expectationResponseCallback, org.mockserver.model.Delay delay) Call method on object locally or remotely (over web socket) to generate response to return when expectation is matched- Parameters:
expectationResponseCallback- object to call locally or remotely to generate response- Returns:
- added or updated expectations
-
forward
public org.mockserver.mock.Expectation[] forward(org.mockserver.model.HttpForward httpForward) Forward request to the specified host and port when expectation is matched- Parameters:
httpForward- host and port to forward to- Returns:
- added or updated expectations
-
forward
public org.mockserver.mock.Expectation[] forward(org.mockserver.model.HttpTemplate httpTemplate) Evaluate Velocity or JavaScript template to generate request to forward when expectation is matched- Parameters:
httpTemplate- Velocity or JavaScript template used to generate response- Returns:
- added or updated expectations
-
forward
public org.mockserver.mock.Expectation[] forward(org.mockserver.model.HttpClassCallback httpClassCallback) Call method on local class in same JVM implementing ExpectationResponseCallback to generate request to forward when expectation is matchedThe callback class must: - implement org.mockserver.mock.action.ExpectationForwardCallback or org.mockserver.mock.action.ExpectationForwardAndResponseCallback - have a zero argument constructor - be available in the classpath of the MockServer
- Parameters:
httpClassCallback- class to callback as a fully qualified class name, i.e. "com.foo.MyExpectationResponseCallback"- Returns:
- added or updated expectations
-
forward
public org.mockserver.mock.Expectation[] forward(org.mockserver.mock.action.ExpectationForwardCallback expectationForwardCallback) Call method on object locally or remotely (over web socket) to generate request to forward when expectation is matched- Parameters:
expectationForwardCallback- object to call locally or remotely to generate request- Returns:
- added or updated expectations
-
forward
public org.mockserver.mock.Expectation[] forward(org.mockserver.mock.action.ExpectationForwardCallback expectationForwardCallback, org.mockserver.mock.action.ExpectationForwardAndResponseCallback expectationForwardResponseCallback) Call method on object locally or remotely (over web socket) to generate request to forward when expectation is matched- Parameters:
expectationForwardCallback- object to call locally or remotely to generate request- Returns:
- added or updated expectations
-
forward
public org.mockserver.mock.Expectation[] forward(org.mockserver.mock.action.ExpectationForwardCallback expectationForwardCallback, org.mockserver.model.Delay delay) Call method on object locally or remotely (over web socket) to generate request to forward when expectation is matched- Parameters:
expectationForwardCallback- object to call locally or remotely to generate request- Returns:
- added or updated expectations
-
forward
public org.mockserver.mock.Expectation[] forward(org.mockserver.mock.action.ExpectationForwardCallback expectationForwardCallback, org.mockserver.mock.action.ExpectationForwardAndResponseCallback expectationForwardResponseCallback, org.mockserver.model.Delay delay) Call method on object locally or remotely (over web socket) to generate request to forward when expectation is matched- Parameters:
expectationForwardCallback- object to call locally or remotely to generate request- Returns:
- added or updated expectations
-
forward
public org.mockserver.mock.Expectation[] forward(org.mockserver.model.HttpOverrideForwardedRequest httpOverrideForwardedRequest) Override fields, headers, and cookies etc in request being forwarded with specified fields, headers and cookies, etc in the specified request when expectation is matched- Parameters:
httpOverrideForwardedRequest- contains request to override request being forwarded- Returns:
- added or updated expectations
-
forwardWithFallback
public org.mockserver.mock.Expectation[] forwardWithFallback(org.mockserver.model.HttpForwardWithFallback httpForwardWithFallback) Forward request to the upstream host and return a fallback response when the upstream returns a configured status code (default 5xx) or times out.- Parameters:
httpForwardWithFallback- the forward-with-fallback action- Returns:
- added or updated expectations
-
error
public org.mockserver.mock.Expectation[] error(org.mockserver.model.HttpError httpError) Return error when expectation is matched- Parameters:
httpError- error to return- Returns:
- added or updated expectations
-
respondWithSse
public org.mockserver.mock.Expectation[] respondWithSse(org.mockserver.model.HttpSseResponse httpSseResponse) -
respondWithLlm
public org.mockserver.mock.Expectation[] respondWithLlm(org.mockserver.model.HttpLlmResponse httpLlmResponse) -
respondWithWebSocket
public org.mockserver.mock.Expectation[] respondWithWebSocket(org.mockserver.model.HttpWebSocketResponse httpWebSocketResponse) -
respondWithGrpcStream
public org.mockserver.mock.Expectation[] respondWithGrpcStream(org.mockserver.model.GrpcStreamResponse grpcStreamResponse) -
respondWithGrpcBidi
public org.mockserver.mock.Expectation[] respondWithGrpcBidi(org.mockserver.model.GrpcBidiResponse grpcBidiResponse) -
respondWithBinary
public org.mockserver.mock.Expectation[] respondWithBinary(org.mockserver.model.BinaryResponse binaryResponse) -
respondWithDns
public org.mockserver.mock.Expectation[] respondWithDns(org.mockserver.model.DnsResponse dnsResponse) -
upsert
public org.mockserver.mock.Expectation[] upsert()Submit the expectation to MockServer without setting any additional primary action. This is the correct terminal call when the expectation's action is already fully defined by itssteps(the responder step defines the action). Using.respond()or.forward()would set a redundant top-level action that conflicts with the steps pipeline.- Returns:
- added or updated expectations
-