- Generate expectations — MockServer reads each operation in the spec and automatically creates a matching expectation with an auto-generated example response.
- Match requests — Use the spec as a request matcher inside an expectation so that only requests conforming to a specific operation are matched.
Generate expectations from an OpenAPI spec
Submit a spec to MockServer using the/mockserver/openapi endpoint. MockServer creates one expectation per operation, with a response action derived from the operation’s example or schema definitions.
Load from a URL
Load from a file path
Load from a classpath location
classpath: scheme prefix — just provide the path relative to the classpath root.
Load from an inline JSON object
Pass the spec content directly in the request body. In the REST API, embed the spec JSON as the value ofspecUrlOrPayload:
Load from an inline YAML string
Pass the YAML spec as a string value forspecUrlOrPayload:
Filtering operations and selecting response codes
UseoperationsAndResponses to control which operations generate expectations and which status code is used for each operation’s response. Keys are operationId values from the spec; values are status code strings ("200", "400", "default", etc.).
operationsAndResponses is omitted, all operations are included and the first response body defined for each operation is used.
Use an OpenAPI spec as a request matcher
Instead of generating expectations, you can use a spec as the request matcher inside a regular expectation. This lets you route all requests that match a given operation to a custom response action.- Java
- JavaScript
- REST API
operationId is omitted, any request matching any operation in the spec will trigger the expectation.
When you use an OpenAPI spec as a request matcher, MockServer internally converts it to a set of request properties matchers. This means operations such as clearing expectations and retrieving active expectations work consistently regardless of whether a request properties matcher or an OpenAPI matcher was used.
Match exactly N times using OpenAPI
Combine an OpenAPI request matcher withtimes to limit how often the expectation fires: