Home Explore Blog CI



docker

1st chunk of `content/guides/wiremock.md`
5f7ba077ff20a857f8f6c1d035ba0bf085735b7d1088e292000000010000101c
---
title: Mocking API services in development and testing with WireMock
description: &desc Mocking API services in development and testing with WireMock
keywords: WireMock, container-supported development
linktitle: Mocking API services with WireMock
summary: *desc
tags: [app-dev, distributed-systems]
languages: [js]
params:
  time: 20 minutes
---

During local development and testing, it's quite common to encounter situations where your app is dependent on the remote APIs. Network issues, rate limits, or even downtime of the API provider can halt your progress. This can significantly hinder your productivity and make testing more challenging. This is where WireMock comes into play. 

WireMock is an open-source tool that helps developers to create a mock server that simulates the behavior of real APIs, providing a controlled environment for development and testing.

Imagine you have both an API and a frontend app, and you want to test how the frontend interacts with the API. Using WireMock, you can set up a mock server to simulate the API's responses, allowing you to test the frontend behavior without relying on the actual API. This can be particularly helpful when the API is still under development or when you want to test different scenarios without affecting the actual API. WireMock supports both HTTP and HTTPS protocols and can simulate various response scenarios, including delays, errors, and different HTTP status codes.

In this guide, you'll learn how to:

- Use Docker to launch up a WireMock container.
- Use mock data in the local development without relying on an external API
- Use a Live API in production to fetch real-time weather data from AccuWeather. 

## Using WireMock with Docker

The official [Docker image for WireMock](https://hub.docker.com/r/wiremock/wiremock) provides a convenient way to deploy and manage WireMock instances. WireMock is available for various CPU architectures, including amd64, armv7, and armv8, ensuring compatibility with different devices and platforms. You can learn more about WireMock standalone on the [WireMock docs site](https://wiremock.org/docs/standalone/docker/).

### Prerequisites

The following prerequisites are required to follow along with this how-to guide:

- [Docker Desktop](https://www.docker.com/products/docker-desktop/)

### Launching WireMock

Launch a quick demo of WireMock by using the following steps:

 1. Clone the [GitHub repository](https://github.com/dockersamples/wiremock-node-docker) locally.

    ```console
    $ git clone https://github.com/dockersamples/wiremock-node-docker
    ```

 2. Navigate to the `wiremock-endpoint` directory

    ```console
    $ cd wiremock-node-docker/
    ```

    WireMock acts as the mock API that your backend will communicate with to retrieve data. The mock API responses have already been created for you in the mappings directory. 

 3. Start the Compose stack by running the following command at the root of the cloned project directory

    ```console
    $ docker compose up -d
    ```

    After a moment, the application will be up and running. 

    ![Diagram showing the WireMock container running on Docker Desktop ](./images/wiremock-using-docker.webp)


    You can check the logs by selecting the `wiremock-node-docker` container:

    ![Diagram showing the logs of WireMock container running on Docker Desktop ](./images/wiremock-logs-docker-desktop.webp)

 4. Test the Mock API.

    ```console
    $ curl http://localhost:8080/api/v1/getWeather\?city\=Bengaluru
    ```
 
    It will return the following canned response with mock data:

    ```plaintext
    {"city":"Bengaluru","temperature":27.1,"conditions":"Mostly cloudy","forecasts":[{"date":"2024-09-02T07:00:00+05:30","temperature":83,"conditions":"Partly sunny w/ t-storms"},{"date":"2024-09-03T07:00:00+05:30","temperature":83,"conditions":"Thunderstorms"},{"date":"2024-09-04T07:00:00+05:30","temperature":83,"conditions":"Intermittent clouds"},{"date":"2024-09-05T07:00:00+05:30","temperature":82,"conditions":"Dreary"},{"date":"2024-09-06T07:00:00+05:30","temperature":82,"conditions":"Dreary"}]}

Title: Mocking API Services in Development and Testing with WireMock
Summary
WireMock is an open-source tool that allows developers to create a mock server simulating real APIs, providing a controlled environment for development and testing. This guide explains how to use Docker to launch a WireMock container, use mock data for local development, and integrate a live API for fetching real-time data in production. It walks through cloning a GitHub repository, launching WireMock with Docker Compose, and testing the mock API endpoint.