WireMock for Java: Complete Guide with Examples

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course
\n

WireMock for Java: Complete Guide to HTTP Mock Server

\n\n
\n

1. What It Is and Why You Need It

\n

WireMock is a powerful library for Java (and other languages via a standalone server) that allows you to create HTTP mock servers for testing. Essentially, it is an emulator of a real HTTP service (API, REST, SOAP) that you can configure to return predefined responses to specific requests. It is an indispensable tool in the arsenal of any developer or tester working with microservice architecture or any systems that depend on external HTTP services.

\n

Why is this needed? In modern development, applications rarely exist in isolation. They communicate with databases, payment gateways, authorization services, weather APIs, and dozens of other external systems. Testing such integrations "live" is a headache: the external service may be unavailable, slow, return unexpected data, or, even worse, charge you for each request. WireMock solves this problem by allowing you to:

\n
    \n
  • Test in isolation: You test only your code, without depending on the availability and stability of external services.
  • \n
  • Simulate rare scenarios: Easily make the mock return 500 errors, timeouts, empty responses, or specific headers — things that are difficult to reproduce with a real service.
  • \n
  • Speed up tests: The mock server responds in milliseconds, while a real service might take seconds to respond.
  • \n
  • Parallelize development: While the backend team hasn't implemented the API yet, the frontend team can already write and test their code using WireMock.
  • \n
\n

WireMock works as a full-fledged HTTP server (based on Jetty) that runs inside your test or as a separate process. It intercepts HTTP requests, compares them with configured rules (stubs), and returns the corresponding response. The library supports record and playback, state verification (verify), template processing (Handlebars), delay simulation, and much more.

\n
\n\n
\n

2. Installation

\n

WireMock for Java is distributed via Maven Central. Add the following dependency to your pom.xml (for Maven) or build.gradle (for Gradle).

\n

Maven (pom.xml):

\n
<dependency>\n    <groupId>org.wiremock</groupId>\n    <artifactId>wiremock</artifactId>\n    <version>3.9.1</version> <!-- Check the latest version on Maven Central -->\n    <scope>test</scope>\n</dependency>
\n

Gradle (build.gradle):

\n
testImplementation 'org.wiremock:wiremock:3.9.1'
\n

Important: Starting from version 3.x, WireMock requires Java 11 or higher. If you are using Java 8, you will need to use version 2.x (e.g., 2.35.1), but it is strongly recommended to upgrade to Java 11+.

\n
\n\n
\n

3. Quick Start — Minimal Working Example

\n

Let's create a simple test that starts WireMock, configures a stub for a GET request to /api/hello, and verifies that the client receives the expected response.

\n
import com.github.tomakehurst.wiremock.WireMockServer;\nimport com.github.tomakehurst.wiremock.client.WireMock;\nimport static com.github.tomakehurst.wiremock.client.WireMock.*;\nimport static org.junit.jupiter.api.Assertions.assertEquals;\nimport org.junit.jupiter.api.Test;\nimport java.net.URI;\nimport java.net.http.HttpClient;\nimport java.net.http.HttpReq                    

Recommendations