Wiremock and how to emulate SOAP Calls

Daniel S. Blanco
3 min readMay 2, 2021

We have already seen several examples of how to use Wiremock (you can see here but in Spanish). And we have always used it to simulate REST calls, today we will see how we can also simulate SOAP calls.

As we are going to see, if we have a little knowledge about Wiremock, it will be a simple task. That helps us to give more details about other things. Like, it’s the creation of the WebService, its client, and then the JUnit test in which we will use Wiremock.

To start, if we want to make a web service we can use the JAX-WS library, in four simple steps:

  • Add the @WebService annotation to the class containing the logic.
  • Add the @WebMethod annotation to each of the methods we want to expose.
  • Create a sun-jaxws.xml file in the WEB-INF folder where the class to be exposed is indicated.
  • Modify the web.xml file to add the WSServlet servlet and the WSServletContextListener listener.

The next step will be to create a client. This can be done even easier, through the JVM wsimport tool and having access to the WSDL file that we will get when deploying the web service. The client code can be obtained with the following command:

wsimport -keep .\BookServiceImplService.wsdl

Once we have our client, it’s time to get down to work and create our JUnit test with Wiremock. The first step is to include the dependency:

<dependency>
<groupId>com.github.tomakehurst</groupId>
<artifactId>wiremock</artifactId>
<version>2.25.1</version>
<scope>test</scope>
</dependency>

Then we configure the WiremockServer in the test class. Through this WiremockServer, we will be able to configure in which port the Wiremock will be listening, which will be the base folder to use or show information through the output console, apart from many more things. Example:

Perhaps the most difficult part when creating the test, is to identify what should be the response to be obtained by the Wiremock. For this we can use the SOAPUI tool, which based on a WSDL allows us to obtain a sample request and response.

Now it is time to do the test. To sum up, the main difference between a REST service and a SOAP service is that the first one sends and receives JSON and the second one XML. Therefore for our tests, we will have to indicate the URL as in a REST service, but indicating that it is going to make a request and receive XML. This can be done through the following steps:

  • Receive a POST call.
  • Indicate that in the Content-Type header we are going to receive ‘text/xml’.
  • Indicate that the SOAPAction header will include the method to be called.
  • The response will be an XML file with the correct format. That we have been able to obtain from SOAPUI.

As you can see, a short and simple post. Especially if you have previous knowledge of Wiremock.

--

--