Skip to main content

WireMock Basics

WireMock is a simulator for HTTP-based APIs. It is often used for testing applications that rely on external APIs by mocking the API responses. WireMock can be used as a standalone server or as a library within your Java project.

Key Features

  • HTTP Response Stubbing: Define expected HTTP responses for specific requests.
  • Request Verification: Verify that specific requests were made.
  • Proxying: Forward requests to other services and capture responses.
  • Record & Playback: Record real API interactions and replay them later.
  • Dynamic Response Generation: Generate responses dynamically based on request parameters.
  • Simulate Faults: Simulate network delays, timeouts, and other faults.

Getting Started

Installation

You can use WireMock in two ways:

  1. Standalone Server:

    • Download the standalone JAR from the official WireMock website.
    • Run the server using the following command:
      java -jar wiremock-standalone-2.27.2.jar --port 8080
    • This will start WireMock on port 8080.
  2. Java Library:

    • Add WireMock as a dependency in your pom.xml (for Maven) or build.gradle (for Gradle).
    • Maven:
      <dependency>
      <groupId>com.github.tomakehurst</groupId>
      <artifactId>wiremock</artifactId>
      <version>2.27.2</version>
      <scope>test</scope>
      </dependency>
    • Gradle:
      testImplementation 'com.github.tomakehurst:wiremock:2.27.2'

Basic Usage

Stubbing Responses

You can define stubs to mock API responses. Here’s an example of how to stub a GET request:

import com.github.tomakehurst.wiremock.client.WireMock;

public class WireMockExample {
public static void main(String[] args) {
WireMock.configureFor("localhost", 8080);
WireMock.stubFor(WireMock.get(WireMock.urlEqualTo("/api/resource"))
.willReturn(WireMock.aResponse()
.withStatus(200)
.withBody("{\"message\": \"Hello, World!\"}")));
}
}

Verifying Requests

You can verify that a specific request was made:

WireMock.verify(WireMock.getRequestedFor(WireMock.urlEqualTo("/api/resource")));

Proxying Requests

You can verify that a specific request was made:

WireMock.stubFor(WireMock.get(WireMock.urlMatching(".*"))
.willReturn(WireMock.aResponse().proxiedFrom("https://www.confusedtester.com")));

Advanced Features

  • Dynamic Templating: Use Handlebars templates to generate dynamic responses.
  • Fault Injection: Simulate network issues by injecting delays or returning error responses.
  • Recording: Record real API interactions and save them as stubs for later use.

Example: Simulating a Delay

WireMock.stubFor(WireMock.get(WireMock.urlEqualTo("/api/resource"))
.willReturn(WireMock.aResponse()
.withStatus(200)
.withBody("{\"message\": \"Hello, Mr B.!\"}")
.withFixedDelay(5000))); // 5-second delay

Conclusion

WireMock is a powerful tool for testing and simulating HTTP-based APIs. Whether you're running it as a standalone server or embedding it in your Java project, WireMock provides a flexible and easy-to-use solution for mocking API responses, verifying requests, and simulating various network conditions.

For more detailed documentation, visit the official WireMock documentation..