How to use WireMock for stubbing web services

WireMock is a library for stubbing and mocking REST Web services. This will be a handy one if we want to test out external web services. For example, you have an application which accesses an external web service. We can use WireMock to mock different responses and test out how our code behaves.

When we use this, it will start a mock HTTP server on the given port number so it looks like we are connecting to the actual web services.

Let’s see how we can use this for writing JUnit test cases. Consider that you have two web services. one if for creating the user and another one is for fetching it. We don’t worry about the actual services. Let’s see how we can stub these services with WireMock.

I am going to use JUnit Rule for this example. Add the below dependencies in the maven pom.xml file.


Now my test code is given below,

package com.wiremock;

import com.github.tomakehurst.wiremock.junit.WireMockRule;
import org.glassfish.jersey.client.JerseyClient;
import org.glassfish.jersey.client.JerseyClientBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;


import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
import static com.github.tomakehurst.wiremock.client.WireMock.get;
import static com.github.tomakehurst.wiremock.client.WireMock.matching;
import static;
import static com.github.tomakehurst.wiremock.client.WireMock.postRequestedFor;
import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo;
import static com.github.tomakehurst.wiremock.client.WireMock.urlMatching;
import static;
import static;
import static org.junit.Assert.assertEquals;

public class WireMockTest {

    public WireMockRule rule = new WireMockRule(8080);

    private static String GET_USERS = "http://localhost:8080/users/12345";
    private static String POST_USER = "http://localhost:8080/users";
    private static String USER_CONTENT = "<users>

    JerseyClient client;

    public void setUp() {
        client = JerseyClientBuilder.createClient();

    public void tearDowm() {

    private void stubFor(){
                aResponse().withHeader("Content-Type", "application/xml")

    public void testAddUser() {
        stubFor();, APPLICATION_XML_TYPE));
                            .withHeader("Content-Type", matching("application/xml")));


    public void testGetUser() {
        Response response =;
        String output = response.readEntity(String.class);
        System.out.println("output: "+output);
        assertEquals(200, response.getStatus());
        assertEquals(USER_CONTENT, output);

The first step is to add the WireMockRule and pass the port number on which the mock server is going to be run. You can specify any port number. By default, it’s 8080.

Now the next step is to add the stubFor for both services(getUser, addUser)
For adding user service, the method is POST and it will return the response status as 200. For getting the user service, the method is GET and it will return the static user response XML.

The next step is to call the services with the REST client object.
The final step is to do the verification. We can do the same kind of verification as such verifying the actual response.

Refer the code @