Open In App

Create Your First Mocked API Endpoint with WireMock

Last Updated : 01 Jun, 2022
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 this article: Introduction to WireMock

In this article, we are going to create our first mocked API endpoint with the help of WireMock and Postman.  

Step By Step Implementation 

Step 1:

Refer to this article Download and Installation of WireMock and run the WireMock as a Standalone Process on your local system. 

Step 2:

After successfully running the WireMock server let’s hit the following endpoint on your local browser and see what happens. 

http://localhost:8080/testapi

So you will get a message like this. Refer to the below image. 

No response could be served as there are no stub mappings in this WireMock instance.

 

You will get the same message in the WireMock console also. Refer to the below image. 

 

So what does it mean? This means there are no mocks being set. So let’s create this mock API “http://localhost:8080/testapi” and then we will hit this API again. 

Step 3:

Open Postman and create a POST request. And put the following URL

http://localhost:8080/__admin/mappings

Note: There is two underscore “_” before the admin. You may consider it like this “_ _admin”

So what we are doing is making a CURL POST request to http://localhost:8080/__admin/mappings and this is the location where all the mappings will be stored for the Wiremock server that we executed or started through the JAR file. 

Step 4: 

And in the Request Body write JSON file as below

{
    "request": {
        "url": "/testapi",
        "method": "GET"
    },
    "response": {
        "status": 200,
        "body": "Welcome to GeeksforGeeks!"
    }
}

So here we are defining request parameters like, url and request method along with the response body in the “response” section. This simply implies that whenever a GET request comes in with URL /testapi then respond with the specified response body i.e “Welcome to GeeksforGeeks!” for this example. And hit the Send button. And in the Response you are going to get something like this

{
    "id": "6197ad08-73d3-4eef-bd79-340c6f5c7145",
    "request": {
        "url": "/testapi",
        "method": "GET"
    },
    "response": {
        "status": 200,
        "body": "Welcome to GeeksforGeeks!"
    },
    "uuid": "6197ad08-73d3-4eef-bd79-340c6f5c7145"
}

Refer to the below image if you are stuck somewhere. 

 

Step 5: 

Now lets hit the same endpoint again (http://localhost:8080/testapi) and see what happens. 

 

Yes! this time we got our result. So we have successfully created our first Mocked API Endpoint with the help of WireMock and Postman. Similarly, you can play around it an there is many things to explore in WireMock and its very very helpful tools for the Developer to perform Integration and Unit testing. 


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

Similar Reads