Home » Java » Enterprise Java » Integrate Wiremock into Spring Boot Java Web Application for Mocking External Dependencies

About Chris Anatalio

Chris Anatalio
Chris is a Software Engineer currently working as a consultant for a firm called Ippon Technologies. He attended Virginia Commonwealth University for a BS in Computer Science. He is active in blogging, social media and open source.

Integrate Wiremock into Spring Boot Java Web Application for Mocking External Dependencies


WireMock is a mock server for HTTP-based APIs. Some might consider it a service virtualization tool or a mock server. It enables you to stub out an API or other external dependency you depend on to expedite local development. It supports testing of edge cases and failure modes that the real API won’t reliably produce. It also is useful in mocking out external dependencies in unit and integration tests. It has excellent integration with jUnit.

Add Wiremock Dependency

First you will want to add the Wiremock dependency. You can download the regular dependency or the fat JAR stand-alone version that contains all it’s dependencies. We will be using the standard dependency here. Add the following dependency to your build.gradle


dependencies {

Add Wiremock Unit test

Here is the complete unit test that you can use for testing your integration with Wiremock. This unit test uses a jUnit4 rule to spin up a Wiremock server on port 8089 and shut it down after each test. We use the stubFor method to define a mocked end point and the response. We use a Spring RestTemplate to create a HTTP request to our mock server and capture the result.


public class WiremockTests {

    RestTemplate restTemplate;
    ResponseEntity response;

    public WireMockRule wireMockRule = new WireMockRule(wireMockConfig().port(8089).httpsPort(8443));

    public void setup() throws Exception {
        restTemplate = new RestTemplate();
        response = null;

    public void givenWireMockAdminEndpoint_whenGetWithoutParams_thenVerifyRequest() {

        RestTemplate restTemplate = new RestTemplate();

        response = restTemplate.getForEntity("http://localhost:8089/__admin", String.class);

        assertThat("Verify Response Body", response.getBody().contains("mappings"));
        assertThat("Verify Status Code", response.getStatusCode().equals(HttpStatus.OK));

    public void givenWireMockEndpoint_whenGetWithoutParams_thenVerifyRequest() {
                        .withHeader("Content-Type", TEXT_PLAIN_VALUE)

        response = restTemplate.getForEntity("http://localhost:8089/api/resource/", String.class);

        assertThat("Verify Response Body", response.getBody().contains("test"));
        assertThat("Verify Status Code", response.getStatusCode().equals(HttpStatus.OK));


You can run this test and if it is complete, you have successfully integrated Wiremock into your application.

Drill down into Unit Test

Here are some static imports you can use for readability and conciseness in your test.


import static com.github.tomakehurst.wiremock.client.WireMock.*;
import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.wireMockConfig;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.springframework.http.MediaType.TEXT_PLAIN_VALUE;

jUnit4 Rule

This jUnit4 @rule will automatically manage the Wiremock server lifecycle and startup and shutdown Wiremock for each test case. You would also be able to implement this using a setup() and teardown() method but the jUnit4 rule is cleaner and more concise.


    public WireMockRule wireMockRule = new WireMockRule(wireMockConfig().port(8089).httpsPort(8443));

Stubbed Endpoint and Response

This code uses the stubFor() method which has been statically imported to define an endpoint, /api/resource/ and the plain text response body “test” You could return a JSON or XML response using this method as well by changing the Content-Type and response body


                        .withHeader("Content-Type", TEXT_PLAIN_VALUE)

Spring RestTemplate

We use a Spring RestTemplate class to execute a GET HTTP request to http://localhost:8089/api/resource/ hitting the stubbed endpoint of the wiremock server. We are expecting a String.class entity response in this case because that is what we defined in the stubFor() method. You would need to define a POJO object to capture a JSON response from your stubbed out method if that is what you configured. We capture the response in a ResponseEntity object which captures the response body, headers and status code as well as other information about the request.


response = restTemplate.getForEntity("http://localhost:8089/api/resource/", String.class);

Starting and Stopping the Wiremock Server Manually

You can start and stop a Wiremock server manually without using a jUnit4 rule to manage the lifecycle. You may want to do this is a bootstrap method when your application starts up.


WireMockServer wireMockServer = new WireMockServer(wireMockConfig().port(8089)); //No-args constructor will start on port 8080, no HTTPS




You now have Wiremock setup in your project. You can use Wiremock in unit and integration tests to stub externally dependencies and also to speed up development in your local environment. You can read more about setting up Wiremock here: http://wiremock.org/docs/getting-started/

(0 rating, 0 votes)
You need to be a registered member to rate this.
2 Comments Views Tweet it!
Do you want to know how to develop your skillset to become a Java Rockstar?
Subscribe to our newsletter to start Rocking right now!
To get you started we give you our best selling eBooks for FREE!
1. JPA Mini Book
2. JVM Troubleshooting Guide
3. JUnit Tutorial for Unit Testing
4. Java Annotations Tutorial
5. Java Interview Questions
6. Spring Interview Questions
7. Android UI Design
and many more ....
I agree to the Terms and Privacy Policy
Notify of

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Newest Most Voted
Inline Feedbacks
View all comments
3 years ago

Hi Cris,excellent tutorial on wire mock. I am new to wire mock and we are implementing this in our project. We need to use wire mock at unit testing. Our API are not exposed yet and we are working on microservices.I am working on GET/POST method currently. Could you please share your GIT repository for this code,or any which I can refer .

Thank you

3 years ago
Reply to  Mukta

Hi Mukta,did you happen to get the git repository for this code or references of how to setup mock server?