WireMock - Verifying with JSON Mappings
Last Updated :
26 Jun, 2022
In Wiremock, Verification comes after stubbing and request matching. Verification is to check with the Wiremock server as to whether a given request was received by it or not. In Wiremock, at least until it is reset the WireMock server records all requests it receives in memory. And this makes it possible to verify that a request matching a specific pattern was received, and also to fetch the requests’ details. Let's demonstrate an example to understand how Verifying works in WireMock with JSON mappings.
Prerequisites:
Step By Step Implementation
Step 1: Run Your Standalone WireMock Server
Open the folder in your local machine where your WireMock JAR file is present. Open the cmd on that directory and hit the following command in order to run the JAR file.
java -jar WireMockJarFileName
If everything is fine then as you can see in the below image our WireMock has been started successfully and the default port number is 8080.
Step 2: Open Postman in Your Local Machine
Go to your Postman app and create a new stub request with the following JSON body.
{
"request": {
"url": "/https/www.geeksforgeeks.org/gfg/1",
"method": "GET"
},
"response": {
"status": 200,
"body": "Welcome to GeeksforGeeks!"
}
}
Refer to the below image if you are stuck somewhere.
Now let’s write the JSON request to verify the request. Create a new POST request to the following URL
http://localhost:8080/__admin/requests/count
And paste the following JSON body
{
"url": "/https/www.geeksforgeeks.org/gfg/1"
}
And in the response body, you can see the following response
{ "count": 1 }
So you can notice that in verification we are doing nothing but getting the count i.e.the number of times the request was sent to the Wiremock server. Similarly, you can play around with it and observe the response that is coming. Similarly, you can try and test the following URL in your Postman.
Some More Concepts Related to WireMock Verifying
Filtering Events
WireMock provides an interesting feature that you can filter the events. That means the results can be filtered to those occurring after a specific date time. For example, if you want the most recent five results after the 18th of June 2022, 11 AM the send the following request
GET : http://localhost:8080/__admin/requests?since=2022-06-18T11:00:00&limit=5
Similarly, the results can also be filtered to include only unmatched requests via a query parameter like the following
GET http://localhost:8080/__admin/requests?unmatched=true
Also, the results can be filtered to include only requests matching a specific stub ID:
GET http://localhost:8080/__admin/requests?matchingStub=b89c8ef7-e5dc-4aa7-9f3d-2914f9054d8f
Criteria Queries
The request journal can also be queried, taking a request pattern as the filter criteria. Try the following request
POST : http://<host>:<port>/__admin/requests/find
Removing Items from the Journal By ID
An individual journal event can be removed via the HTTP API by the following request
DELETE : http://<host>:<port>/__admin/requests/{id}
Resetting the Request Journal
The request log can be reset at any time. If you’re using either of the JUnit rules this will happen automatically at the start of every test case. However, you can do it yourself by the following request
DELETE : http://<host>:<port>/__admin/requests
Similar Reads
WireMock - Stubbing with JSON Mappings WireMock is a tool for mocking HTTP-based APIs that runs in the unit tests, on the desktop, or in the test environment. We can also say it is a simulator for HTTP-based APIs, considered a service virtualization tool or a mock server. It enables you to stay productive when an API you depend on Doesn'
4 min read
WireMock - Verifying with JUnit Test In Wiremock, Verification comes after stubbing and request matching. Verification is to check with the Wiremock server as to whether a given request was received by it or not. In Wiremock, at least until it is reset the WireMock server records all requests it receives in memory. And this makes it po
4 min read
WireMock - Stub Priority with JSON Mappings In WireMock, Stub priority allows us to assign priorities for the request mappings. That means itâs a simple way to resolve which stub mapping should be given more precedence if a request matches 2 or more stub mappings. Sometimes, youâll want to declare two or more stub mappings that âoverlapâ, in
4 min read
WireMock - Request Matching with JSON Mappings WireMock is a tool for mocking HTTP-based APIs that runs in the unit tests, on the desktop, or in the test environment. Read more about WireMock in this article: Introduction to WireMock. One of the most important features of WireMock is Request Matching. WireMock supports matching of requests to st
4 min read
WireMock - Proxying with JUnit Test WireMock has the power to selectively proxy requests through to other hosts. This supports a proxy setup where requests are by default proxied to another service, but where specific stubs are configured these are returned in place of the remote serviceâs response.  Let's say there are 2 services, S
4 min read
WireMock - URL Matching with Regex WireMock is a tool for mocking HTTP-based APIs that runs in the unit tests, on the desktop, or in the test environment. Read more about WireMock in the article Introduction to WireMock. In this article, we are going to discuss one of the important concepts in Wiremock, i.e how to match the URL with
4 min read