How to mock an API in WireMock using Docker

How to mock an API in WireMock using Docker

In this blog, we will be learning how to create a service virtualization using WireMock. When we are developing a micro-service application, one service may invoke an API call to another service. Let's consider we are part of a food order application development. In that, we are developing a service that is called by UI(frontend) to get the restaurant list and the restaurant's menu items. We will name this service as Restaurant Service and another service as Menu Service.

In the above diagram, you can see that the restaurant service invokes an API call to the menu service to get the list of menu items. In case the menu service is not yet ready to handle the API calls to send the menu list what happens next implementation of the restaurant service will be blocked and we can't build and test the restaurant service till the menu service is ready.

Here is where the WireMock comes into the picture using wiremock we can mock the Menu service APIs and continue our implementation of the restaurant service.


Now, What is WireMock?

The flexible tool for building mock APIs.

WireMock is an HTTP mock server. At its core, it is a web server that can be primed to serve canned responses to particular requests (stubbing) and captures incoming requests so that they can be checked later (verification).


Stubbing & Request Matching

Stubbing is the core concept of WireMock that enables us to return a mocked or static or pre-configured response for a request made to the WireMock server.

Given below is the process or sequence that is followed

  • First stubs are configured on a WireMock server

  • Now, when a request is sent to the WireMock server, then the request matching login executes.

  • If the matcher matches any stub, then the result is sent back to the matched or configured response

  • Else an error message stating that no match/stub is found.

What Response Parameters Can Be Stubbed?

For a request, we can stub all the parameters of a response as shown below

  1. Status Code

  2. Response Headers

  3. Response Body etc,

Now, we are going to use docker-compose for installing and running the WireMock server.

Installation of Docker Desktop

Before downloading the docker desktop please read out the system requirements mentioned on the docker website. Here we are not going to cover the docker installation process, we do have a lot to cover on the WireMock.

If docker is new to you please get some knowledge on docker and docker-compose by looking at these YouTube tutorials. This is pretty much good enough to work in docker and docker-compose.

Docker Tutorial for Beginners

Docker Compose Tutorial

Getting Started with WireMock API Stubbing

Create a root directory for your project like C:/HelloWorld

  • Inside the root directory create a file called "docker-compose.yml" and mention the following content in that file.
services:
    wiremock:
        container_name: HelloWorld
        image: wiremock/wiremock:latest
        ports:
            - "8080:8080"
        volumes:
            - ./__files/:/home/wiremock/__files/
            - ./mappings/:/home/wiremock/mappings/

- image: image is the WireMock official docker image. You can check the version details in here https://hub.docker.com/r/wiremock/wiremock

- ports: ports is the wiremock http/https server port. WireMock exposes 8080 for HTTP 8443 for HTTPS.

- volumes: volume "/home/wiremock" used by the wiremock for data storage

  • In the command line, run the following command in order to download the wiremock docker image and start the wiremock server

      > docker-compose up
    

    This is will pull you the wiremock image from docker hub, starts the wiremock server and creates the folders (/__files and /mappings) for you.

    Pullings the docker image.

    Started the WireMock server in port 8080

    Created the folders for me.

  • In the mappings directory, just create a JSON file called "hello.json" and do a stubbing like below

      {
          "request": {
              "method": "GET",
              "url": "/hello"
          },
          "response": {
              "status": 200,
              "body": "Hello World!"
          }
      }
    
  • Now, stop the WireMock server by pressing "CTRL + C" on Windows/Linux, "Command + ." (dot/period) on Mac and start again by "docker-compose up".

  • This time image won't be pulled from the docker hub. It will start the WireMock server for you and register the stubbing which is mentioned in the "/mappings" directory.

  • Once the server is started you can invoke an API call by hitting "localhost:8080/hello", you can see the stubbed response as a return.

    If you wanted to learn more about WireMock and How to do stubbing for other HTTP methods like GET, GET by Id, POST, do check out this YouTube playlist WireMock Camp.