This section will cover how to manually configure a WireMock server, i.e., without the support of JUnit auto-configuration. We demonstrate the usage with a very simple stub.
3.1. Server Setup
First, we instantiate a WireMock server:
WireMockServer wireMockServer = new WireMockServer(String host, int port);
In case no arguments are provided, the server host defaults to localhost and the server port to 8080.
Then we can start and stop the server using two simple methods:
wireMockServer.start();
and:
wireMockServer.stop();
3.2. Basic Usage
We’ll first demonstrate the WireMock library with a basic usage, where a stub for an exact URL without any further configuration is provided.
Let’s create a server instance:
WireMockServer wireMockServer = new WireMockServer();
The WireMock server must be running before the client connects to it:
wireMockServer.start();
The web service is then stubbed:
configureFor("localhost", 8080);
stubFor(get(urlEqualTo("/baeldung")).willReturn(aResponse().withBody("Welcome to Baeldung!")));
This tutorial makes use of the Apache HttpClient API to represent a client connecting to the server:
The following code verifies that the server has got a request to the expected URL and the response arriving at the client is exactly what was sent:
verify(getRequestedFor(urlEqualTo("/baeldung")));
assertEquals("Welcome to Baeldung!", stringResponse);
Finally, we should stop the WireMock server to release system resources:
wireMockServer.stop();
4. JUnit Managed Server
In contrast to Section 3, this section illustrates the usage of a WireMock server with the help of JUnit Rule.
4.1. Server Setup
We can integrate a WireMock server into JUnit test cases by using the @Rule annotation. This allows JUnit to manage the life cycle, starting the server prior to each test method and stopping it after the method returns.
Similar to the programmatically managed server, a JUnit managed WireMock server can be created as a Java object with the given port number:
@Rule
public WireMockRule wireMockRule = new WireMockRule(int port);
If no arguments are supplied, server port will take the default value, 8080. Server host, defaulting to localhost, and other configurations may be specified using the Options interface.
4.2. URL Matching
After setting up a WireMockRule instance, the next step is to configure a stub.
In this subsection, we will provide a REST stub for a service endpoint using regular expression:
The previous subsections deal with situations where an HTTP request matches only a single stub.
It’s more complicated if there is more than a match for a request. By default, the most recently added stub will take precedence in such a case.
However, users can customize that behavior to take more control of WireMock stubs.
We will demonstrate operations of a WireMock server when a coming request matches two different stubs, with and without setting the priority level, at the same time.
Both scenarios will use the following private helper method:
This article introduced WireMock and how to set up as well as configure this library for testing of REST APIs using various techniques, including matching of URL, request headers and body.
The implementation of all the examples and code snippets can be found in .