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)
);
var mockServerClient = require('mockserver-client').mockServerClient;
mockServerClient("localhost", 1080)
.verify(
{ 'path': '/some/path' },
2
)
.then(
function () { console.log("request found at least 2 times"); },
function (error) { console.log(error); }
);
curl -v -X PUT "http://localhost:1080/mockserver/verify" -d '{
"httpRequest": {
"path": "/some/path"
},
"times": {
"atLeast": 2
}
}'
At most N times
new MockServerClient("localhost", 1080)
.verify(
request().withPath("/some/path"),
VerificationTimes.atMost(2)
);
curl -v -X PUT "http://localhost:1080/mockserver/verify" -d '{
"httpRequest": { "path": "/some/path" },
"times": { "atMost": 2 }
}'
Exactly N times
new MockServerClient("localhost", 1080)
.verify(
request().withPath("/some/path"),
VerificationTimes.exactly(2)
);
mockServerClient("localhost", 1080)
.verify({ 'path': '/some/path' }, 2, 2)
.then(
function () { console.log("request found exactly 2 times"); },
function (error) { console.log(error); }
);
curl -v -X PUT "http://localhost:1080/mockserver/verify" -d '{
"httpRequest": { "path": "/some/path" },
"times": { "atLeast": 2, "atMost": 2 }
}'
Never received
new MockServerClient("localhost", 1080)
.verify(
request().withPath("/some/path"),
VerificationTimes.exactly(0)
);
curl -v -X PUT "http://localhost:1080/mockserver/verify" -d '{
"httpRequest": { "path": "/some/path" },
"times": { "atMost": 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 any operation at least twice
mockServerClient("localhost", 1080)
.verify(
{
'specUrlOrPayload': 'https://raw.githubusercontent.com/mock-server/mockserver/master/mockserver-integration-testing/src/main/resources/org/mockserver/openapi/openapi_petstore_example.json'
},
2
);
// verify a specific operation exactly once
mockServerClient("localhost", 1080)
.verify(
{
'specUrlOrPayload': 'org/mockserver/openapi/openapi_petstore_example.json',
'operationId': 'showPetById'
},
1, 1
);
# at least twice
curl -v -X PUT "http://localhost:1080/mockserver/verify" -d '{
"httpRequest": {
"specUrlOrPayload": "https://raw.githubusercontent.com/mock-server/mockserver/master/mockserver-integration-testing/src/main/resources/org/mockserver/openapi/openapi_petstore_example.json"
},
"times": { "atLeast": 2 }
}'
# specific operation exactly once
curl -v -X PUT "http://localhost:1080/mockserver/verify" -d '{
"httpRequest": {
"specUrlOrPayload": "org/mockserver/openapi/openapi_petstore_example.json",
"operationId": "showPetById"
},
"times": { "atLeast": 1, "atMost": 1 }
}'
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()
);
mockServerClient("localhost", 1080)
.verifyById(
{ 'id': '31e4ca35-66c6-4645-afeb-6e66c4ca0559' },
1, 1
);
curl -v -X PUT "http://localhost:1080/mockserver/verify" -d '{
"id": "31e4ca35-66c6-4645-afeb-6e66c4ca0559"
}'
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")
);
var mockServerClient = require('mockserver-client').mockServerClient;
mockServerClient("localhost", 1080)
.verifySequence(
{ 'path': '/some/path/one' },
{ 'path': '/some/path/two' },
{ 'path': '/some/path/three' }
)
.then(
function () { console.log("sequence found in order"); },
function (error) { console.log(error); }
);
curl -v -X PUT "http://localhost:1080/mockserver/verifySequence" -d '{
"httpRequests": [
{ "path": "/some/path/one" },
{ "path": "/some/path/two" },
{ "path": "/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"
)
);
mockServerClient("localhost", 1080)
.verifySequence(
{ 'path': '/status' },
{
'specUrlOrPayload': 'org/mockserver/openapi/openapi_petstore_example.json',
'operationId': 'listPets'
},
{
'specUrlOrPayload': 'org/mockserver/openapi/openapi_petstore_example.json',
'operationId': 'showPetById'
}
);
curl -v -X PUT "http://localhost:1080/mockserver/verifySequence" -d '{
"httpRequests": [
{ "path": "/status" },
{
"specUrlOrPayload": "org/mockserver/openapi/openapi_petstore_example.json",
"operationId": "listPets"
},
{
"specUrlOrPayload": "org/mockserver/openapi/openapi_petstore_example.json",
"operationId": "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.