Debugging WireMock calls when using JUnit WireMockRule

Mocking using the WireMockRule in your JUnit test classes and struggle with 404’s?

It is not that trivial to find in the WireMock documentation but it is in there, under ‘Listening for requests’ @ Plain debugging fine, but sometimes one really wants to know the details of the calls made to the underlying services that are consumed, especially when WireMocking these services and there is a fine grained matching mechanism to deal with.

Below is the quick awesome tip to get the details you need to resolve the WireMock returned 404’s easily.

Add a request listener to your WireMockRule and Use Java 8 lambdas to smoothly implement the WireMock interface RequestListener that has the single method requestReceived(Request request, Response response). Print out the reponse and request details you want. Run your tests and check the print outs, all set!

import com.github.tomakehurst.wiremock.junit.WireMockRule;

public class Test {

   public WireMockRule wireMockRule = new WireMockRule(6969);

   public void setupTest() {
      wireMockRule.addMockServiceRequestListener((request, response) -> {
         System.out.println("URL Requested => " + request.getAbsoluteUrl());
         System.out.println("Request Body => " + request.getBodyAsString());
         System.out.println("Request Headers => " + request.getAllHeaderKeys());
         System.out.println("Response Status => " + response.getStatus());
         System.out.println("Response Body => " + response.getBodyAsString());

One Response to “Debugging WireMock calls when using JUnit WireMockRule”

  1. Richard Peters Says:

    Thanks for that – a nice simple straight forward solution. Given my circumstances I had to use a subtly different solution – this is what worked for me.

    DigitalWireMockRule.getWireMockServer().addMockServiceRequestListener(new RequestListener() {

    public void requestReceived(Request request, Response response) {
    System.out.println(“URL Requested : ” + request.getAbsoluteUrl());
    System.out.println(“Request Body : ” + request.getBodyAsString());
    System.out.println(“Request Headers : ” + request.getAllHeaderKeys());
    System.out.println(“Response Status : ” + response.getStatus());
    System.out.println(“Response Body : ” + response.getBodyAsString());

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: