Skip to main content
MockServer records every request it receives — both requests that matched an expectation and requests that were proxied. Use verification to assert after the fact that specific requests arrived, and in what quantity or order.

Verify a single request

Call verify with a request matcher and a times condition.

At least N times

new MockServerClient("localhost", 1080)
    .verify(
        request()
            .withPath("/some/path"),
        VerificationTimes.atLeast(2)
    );

At most N times

new MockServerClient("localhost", 1080)
    .verify(
        request().withPath("/some/path"),
        VerificationTimes.atMost(2)
    );

Exactly N times

new MockServerClient("localhost", 1080)
    .verify(
        request().withPath("/some/path"),
        VerificationTimes.exactly(2)
    );

Never received

new MockServerClient("localhost", 1080)
    .verify(
        request().withPath("/some/path"),
        VerificationTimes.exactly(0)
    );

Verify using an OpenAPI matcher

Use an OpenAPI spec as the request matcher in a verification call. This is useful when you want to verify that a request conforming to a specific API operation was received.
// verify any operation in the spec was called at least twice
new MockServerClient("localhost", 1080)
    .verify(
        openAPI(
            "https://raw.githubusercontent.com/mock-server/mockserver/master/mockserver-integration-testing/src/main/resources/org/mockserver/openapi/openapi_petstore_example.json"
        ),
        VerificationTimes.atLeast(2)
    );

// verify a specific operation was called exactly once
new MockServerClient("localhost", 1080)
    .verify(
        openAPI(
            "org/mockserver/openapi/openapi_petstore_example.json",
            "showPetById"
        ),
        VerificationTimes.once()
    );

Verify by expectation ID

If you assigned an id to an expectation, you can verify by that ID directly:
new MockServerClient("localhost", 1080)
    .verify(
        "31e4ca35-66c6-4645-afeb-6e66c4ca0559",
        VerificationTimes.once()
    );

Verify a sequence of requests

Use verifySequence (or pass multiple request matchers to verify in Java) to assert that a set of requests arrived in order. Each request in the sequence must have been received at least once, and each must have been received after the previous one.
new MockServerClient("localhost", 1080)
    .verify(
        request().withPath("/some/path/one"),
        request().withPath("/some/path/two"),
        request().withPath("/some/path/three")
    );

Mix request matchers and OpenAPI matchers in a sequence

new MockServerClient("localhost", 1080)
    .verify(
        request().withPath("/status"),
        openAPI(
            "org/mockserver/openapi/openapi_petstore_example.json",
            "listPets"
        ),
        openAPI(
            "org/mockserver/openapi/openapi_petstore_example.json",
            "showPetById"
        )
    );

Retrieve recorded requests for debugging

Retrieve all requests MockServer has recorded so you can inspect what arrived:
curl -v -X PUT "http://localhost:1080/mockserver/retrieve?type=REQUESTS" -d '{
    "httpRequest": {
        "path": "/some/path"
    }
}'
Retrieve recorded requests as Java code or JSON:
# as JSON (default)
curl -v -X PUT "http://localhost:1080/mockserver/retrieve?type=REQUESTS&format=JSON"

# as Java
curl -v -X PUT "http://localhost:1080/mockserver/retrieve?type=REQUESTS&format=JAVA"
Retrieve active expectations (useful for confirming which expectations are still registered):
curl -v -X PUT "http://localhost:1080/mockserver/retrieve?type=ACTIVE_EXPECTATIONS"
If a verification assertion fails unexpectedly, retrieve recorded requests to see exactly what MockServer received. This can reveal URL encoding differences, unexpected headers, or missing request bodies.