WireMock: Easy API Mocking

Pankaj Dusane
helpshift-engineering
4 min readAug 7, 2023

Testing applications that rely on external APIs can be a challenging task, especially when dealing with unpredictable responses, unavailability of services, or the need to test specific scenarios.

WireMock, a powerful HTTP-based mock server, comes to the rescue. It offers a flexible and efficient solution to simulate API responses, enabling developers and testers to create reliable test environments.

In this blog post, we will delve into the world of WireMock for API request mocking, exploring its features, benefits, and how it can revolutionise your testing process.

What is WireMock?

WireMock is an open-source Java library that allows you to create virtual HTTP services, also known as stubs, which respond to API requests. It offers a RESTful API for defining custom request matching criteria and corresponding responses. With WireMock, you can mimic real-world API behaviour, isolate yourself from flakey 3rd party APIs, ensuring consistent, controlled, and reproducible test environments.

Key Features of WireMock:

  1. Easy Setup and Configuration: WireMock can be set up quickly with minimal effort, making it an excellent choice for developers and testers.
  2. Flexible Response Definitions: You can define static responses or dynamic responses based on request parameters, headers, and other factors.
  3. Record and Playback: WireMock has a record and playback feature, allowing you to capture live API interactions and later replay them as stubs.
  4. Request Matching: It provides powerful request matching options, enabling you to precisely specify which API requests the stub should respond to.
  5. Stateful Behaviour: WireMock supports stateful interactions, allowing you to simulate session-like behaviour or sequences of API calls.
  6. HTTPS Support: It can handle both HTTP and HTTPS requests, providing a secure testing environment.

Why Use WireMock for API Request Mocking?

  1. Isolated Testing: WireMock allows you to test your application independently of external APIs, reducing the risk of false positives or false negatives during testing.
  2. Test Scenarios and Edge Cases: You can create specific test scenarios, including edge cases and error conditions, to ensure your application handles them gracefully.
  3. Speed and Reliability: Mocking API requests with WireMock eliminates external dependencies, making your tests faster and more reliable.
  4. Reproducible Tests: By defining explicit responses for each API request, you can reproduce test scenarios consistently, ensuring test stability.

Getting Started with WireMock:

Now, we would be looking at running wiremock as a standalone process:

  1. Download the wiremock standalone jar file from here
  2. Once you have downloaded the jar file, you can start the server simply by doing:
java -jar wiremock-jre8-standalone-2.35.0.jar

When the WireMock server starts, it creates two directories under the current one: mappings and __files.

3. Create a file with a .json extension under mappings with the following content:

{
"request": {
"method": "GET",
"url": "/api/mytestapi"
},
"response": {
"status": 200,
"body": "This an example response!"
}
}

4. Restart the wiremock server and try hitting a GET request to the above mentioned request.url:

curl http://localhost:8080/api/mytestapi

After the request is sent to the server, it matches the incoming request with the mappings present in mappings folder. Once the request matches, it returns the corresponding mapped response for the request.

One thing to note here is that all the .json mappings should be unique in some or the other way, otherwise multiple mappings might satisfy the incoming request and error would be returned.

A request can be made unique through the use of different available attributes, few of them are:

i. request.path - URL path

ii. request.host- hostname part of the URL e.g. my.mock.com

iii. request.headers.<key>- First value of a request header e.g. request.headers.X-Requested-With

iv. request.body - Request body text

Response could also be returned in the form of a .json file which needs to be placed in the __files folder created by the server and the mapping would look something like:

{
"request": {
"method": "GET",
"url": "/api/mytestapi"
},
"response": {
"status": 200,
"bodyFileName": "files/myresponse.json"
}
}

5. Once the work is done, server can be shutdown by either stopping the standalone server running in the terminal or by hitting a post request with an empty body to http://<host>:<port>/__admin/shutdown.

WireMock is a valuable tool for API request mocking, enabling developers and testers to create reliable and controlled test environments. By simulating API responses, WireMock empowers teams to isolate their tests, handle edge cases, and speed up test execution while ensuring reproducibility. Incorporating WireMock into your testing process can lead to more efficient and robust testing, ultimately resulting in higher-quality applications. So, embrace the power of WireMock and elevate your testing game today! Happy testing!

--

--