When developing or testing an application that relies on external APIs, it’s often useful to have a mock server to simulate the behavior of those APIs. WireMock is a popular tool for creating such mock servers. In this post, we’ll walk through the basics of setting up a simple mock server using WireMock in Docker.

What is WireMock?

WireMock is a flexible tool for mocking HTTP services. It allows you to create a mock server that can simulate the behavior of a real API, making it easier to develop and test your application without relying on the actual external service.

Prerequisites

To follow along with this tutorial, you’ll need:

  • Docker installed on your machine
  • Basic understanding of Docker commands

Getting Started

1. Pulling the WireMock Docker Image

The easiest way to get started with WireMock in Docker is by using the official WireMock Docker image. You can pull the latest version of the image from Docker Hub:

docker pull wiremock/wiremock

2. Running WireMock in Docker

Once the image is pulled, you can run WireMock as a Docker container. By default, WireMock runs on port 8080. You can start the container with the following command:

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

This command runs WireMock in detached mode (-d) and maps port 8080 of the container to port 8080 on your local machine (-p 8080:8080). Easy peasy, right? George

3. Creating Mappings

WireMock uses mappings to define the behavior of the mock server. Mappings are usually defined in JSON files. Here’s an example of a simple mapping that responds to a GET request at /hello with a JSON message.

Create a directory named wiremock on your local machine, and inside it, create a directory named mappings. Then, create a file named hello-world.json inside the mappings directory with the following content:

{
"request": {
"method": "GET",
"url": "/hello"
},
"response": {
"status": 200,
"body": "{"message": "Hello, World!"}",
"headers": {
"Content-Type": "application/json"
    }
  }
}

4. Running WireMock with Custom Mappings

To run WireMock with your custom mappings, you need to mount the local directory containing your mappings to the container. Use the following command:

docker run -d -p 8080:8080 -v $(pwd)/wiremock:/home/wiremock --name wiremock wiremock/wiremock

This command mounts the local wiremock directory to /home/wiremock in the container.

5. Verifying the Mock Server

Once WireMock is running, you can verify your mock server by sending a request to it. You can use curl or any HTTP client of your choice:

curl http://localhost:8080/hello

You should receive the following response:

json
{
  "message": "Hello, World!"
}

6. Managing the WireMock Container

You can manage the WireMock container using standard Docker commands. For example, to stop the container, use:

docker stop wiremock

To remove the container, use:

docker rm wiremock

Conclusion

Using WireMock in Docker is a powerful way to create mock servers for testing and development. By running WireMock in a Docker container, you can easily manage and isolate your mock server environment, ensuring consistency and reproducibility across different stages of development and testing.

Whether you are simulating simple API responses or complex interactions, WireMock provides a robust solution for managing external service dependencies. Happy mocking!

πŸ’­ Note: The examples provided in this blog post are for illustrative purposes. Adjust the versions and configurations as needed for your specific setup.