Open In App

WireMock – Verifying with JSON Mappings

Last Updated : 26 Jun, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

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": "/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": "/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

Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads