WireMock

API Mocking made easy!

·

4 min read

WireMock

In today’s world of microservices and distributed architectures, testing APIs effectively has become essential to ensure system stability and functionality. This is where WireMock shines as a versatile tool that allows developers and testers to simulate HTTP services. By mocking APIs and web services, WireMock helps in controlling the behavior of dependencies in a test environment without relying on real external services.

In this blog, we’ll dive deep into what WireMock is, its key features, and how you can get started with it for API testing.

What is WireMock?

WireMock is a flexible HTTP-based tool for mocking, stubbing, and simulating the behavior of external APIs. It allows you to create, configure, and test interactions between services by replacing real HTTP endpoints with simulated responses.

WireMock is typically used to:

  • Mock external APIs that your service depends on.

  • Simulate failures or delays in external systems.

  • Test services in isolation without needing to access real endpoints.

  • Record and replay interactions between services.

By creating stubs for various API responses, WireMock makes it possible to perform controlled testing where you can validate how your system reacts to different scenarios.

Key Features of WireMock

  1. Stubbing Responses
    You can set up different HTTP responses for various requests, including headers, status codes, and response bodies. This allows you to create specific conditions to test your API’s behavior.

  2. Request Matching
    WireMock can match requests based on criteria such as URL, headers, query parameters, and request bodies. This makes it easy to test your system’s behavior under different input conditions.

  3. Record and Replay
    One powerful feature of WireMock is its ability to record HTTP interactions with live services and then replay them later for testing. This is particularly useful when testing against third-party APIs.

  4. Fault Simulation
    WireMock allows you to simulate network latency, timeouts, or even service outages, making it an ideal tool for resilience testing.

  5. Extensibility
    WireMock provides extension points to add custom logic for request matching and response generation, enabling more advanced testing scenarios.

  6. Easy Integration
    WireMock integrates seamlessly with popular testing frameworks such as JUnit, TestNG, and even continuous integration tools. It’s available as a standalone server, a Docker image, or can be embedded into your Java or C# tests as a library.

Install WireMock via Docker

Using Docker makes it easy to get started with WireMock without the hassle of managing dependencies or installation. Here’s how you can set up WireMock as a standalone service.

  1. Install Docker (if not already installed) by following instructions from the official Docker website.

  2. Run WireMock as a Docker Container:

    Open your terminal and run the following command to pull the WireMock Docker image and start the container:

     docker run -d --name wiremock -p 8080:8080 wiremock/wiremock
    

    This command:

    • Pulls the WireMock image from Docker Hub.

    • Runs WireMock on port 8080.

Verify the Installation:

Once the container is up and running, verify that WireMock is working by hitting the default /__admin endpoint in your browser or using a tool like curl or using Postman.

curl http://localhost:8080/__admin/

You should see the below response:

Now, you’re ready to start mocking!

Stubbing an API

With WireMock up and running, the first step is to stub an API. This means setting up WireMock to return a predefined response when a specific request is made.

  1. Creating a Simple Stub:

    Let’s create a stub for a simple GET request to /api/cart. We’ll use a curl command to send this stub definition to WireMock’s mappings.

     curl -X POST http://localhost:8080/__admin/mappings \
     -H "Content-Type: application/json" \
     -d '{
         "request": {
             "method": "GET",
             "url": "/api/cart"
         },
         "response": {
             "status": 200,
             "body": "{\"items\": [{\"itemId\": \"123\", \"quantity\": 2}], \"total\": 50}",
             "headers": {
                 "Content-Type": "application/json"
             }
         }
     }'
    

    This creates a stub mapping inside Wiremock’s journal in memory.

    Here is how it looks from postman:

  2. Testing the Stub:

    Now that the stub is set up, test it by sending a GET request to /api/cart:

     curl http://localhost:8080/api/cart
    

    You should get the following response:

     {
       "items": [
         {
           "itemId": "123",
           "quantity": 2
         }
       ],
       "total": 50
     }
    

    Here is how it looks from postman:

    This demonstrates how WireMock can easily simulate an API response, replacing the need for a real endpoint. This wiremock endpoint can be used in place of actual API endpoint in various scenarios like when calling to display the cart items in frontend without depending on the actual API Or when testing a different API dependent on the cart API to get the cart items we can use this wiremock endpoint to return the mocked response without blocking the testing.