Open In App

WireMock – Stubbing with JSON Mappings

Improve
Improve
Like Article
Like
Save
Share
Report

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


Last Updated : 26 Jun, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads