Create Your First Mocked API Endpoint with WireMock
Last Updated :
01 Jun, 2022
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": "/https/www.geeksforgeeks.org/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": "/https/www.geeksforgeeks.org/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.
Similar Reads
WireMock - Stub Priority with JUnit Test 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. It is sometimes the case that youâll want to declare two or more stub mappings
4 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
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
How to Use WireMock 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. 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 - 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 - 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