WireMock - Stubbing with JSON Mappings
Last Updated :
06 Jan, 2025
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't exist and, Isn't complete or, Costly to access. It supports the testing of Edge cases and failure modes. It's fast so reduces build time significantly. In simple terms, Wiremock is a mocking setup for integration testing. It is mainly used during the development and more significantly during the Integration testing while a system or service talks to one or multiple external or internal dependencies/services.
Read more about WireMock in the following article: Introduction to WireMock
One of the most important features of WireMock is Stubbing. Stubbing is the core feature of WireMock and it has the ability to return canned HTTP responses for requests matching criteria. So in this article, we are going to see how Stubbing works with JSON Mappings in WireMock.
Prerequisites:
1. Basic Stubbing for the GET Request
Open your Postman and send a POST request to the following URL
http://<host>:<port>/__admin/mappings
with the following JSON request body
{
"request": {
"method": "GET",
"url": "put your url as per your requirement"
},
"response": {
"status": 200,
"body": "put your body here"
}
}
Example
For example in this article our URL is
URL:
http://localhost:8080/__admin/mappings
And the JSON request body is
Request Body:
{
"request": {
"url": "/https/www.geeksforgeeks.org/gfg/user1",
"method": "GET"
},
"response": {
"status": 200,
"body": "Welcome to GeeksforGeeks!"
}
}
Click on the Send button now and in the Response, you can see the following Response Body with the status code 201 Created.
Response Body:
{
"id": "609c2585-47f9-456e-b842-51db4411cbb7",
"request": {
"url": "/https/www.geeksforgeeks.org/gfg/user1",
"method": "GET"
},
"response": {
"status": 200,
"body": "Welcome to GeeksforGeeks!"
},
"uuid": "609c2585-47f9-456e-b842-51db4411cbb7"
}
Please refer to the below image if you are stuck somewhere
Now, let's test our Stubbed API. Again open a new tab in Postman and send a GET request to the following URL
http://localhost:8080/gfg/user1
And you are going to get the following response in the Response body.
Welcome to GeeksforGeeks!
Please refer to the below image if you are stuck somewhere
In case you have hit the wrong endpoint then you are going to get a response something like this with the 404 Not Found error code. Suppose instead of hitting the endpoint http://localhost:8080/gfg/user1, we have hit the following endpoint http://localhost:8080/gfg/user2, then we are going to get the response like this
And you can see the same thing in your WireMock console also
2. Basic Stubbing for the POST Request
Similar to the GET request just change the values of "request" -> "method" to "POST" in the JSON request body and you are done. Refer to the below JSON code.
{
"request": {
"method": "GET",
"url": "put your url as per your requirement"
},
"response": {
"status": 200,
"body": "put your body here"
}
}
3. Sending Response Headers Along with Stub
We can also send a response header along with the Stub. Refer to the below JSON code for the syntax.
{
"request": {
"method": "GET",
"url": "put your url as per your requirement"
},
"response": {
"status": 200,
"body": "put your body here",
"headers": {
"Content-Type": "text/plain",
"Cache-Control": "no-cache"
}
}
}
4. Setting the Response Status Message
In addition to the status code, the status message can optionally also be set. Refer to the below JSON code for the syntax.
{
"request": {
"method": "GET",
"url": "put your url as per your requirement"
},
"response": {
"status": 200,
"statusMessage": "put your response status message here"
}
}
5. Saving Stubs
Stub mappings that have been created can be persisted in the mappings directory via posting a request with an empty body to the following URL
http://<host>:<port>/__admin/mappings/save
6. Editing Stubs
Existing stub mappings can be modified, provided they have been assigned an ID. To do it send a PUT request to the following URL
http://<host>:<port>/__admin/mappings/{id}
with the following JSON request body
{
"request": {
"method": "GET",
"url": "put your modified url as per your requirement"
},
"response": {
"status": 200,
"body": "put your modified body here"
}
}
Please refer to the below image if you are stuck somewhere
7. Deleting Stubs
The stub can be deleted via the HTTP API by using the DELETE request to the following URL
http://<host>:<port>/__admin/mappings/{id}
where id is the UUID of the stub mapping, found in its id field.
Similar Reads
WireMock - Verifying with JSON Mappings 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
3 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 - Record Stub Mappings WireMock can create stub mappings from requests it has received. Wiremock provides a recorder interface to record or capture stub mappings by proxying through an actual URL. This is helpful for creating mappings automatically without having to manually configure or create mappings in the Wiremock se
3 min read
WireMock - Request Matching with JUnit Test 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
5 min read