My Shitty Code

Embracing the Messiness in Search of Epic Solutions

Spring Web Services: Client Integration Testing with MockWebServiceServer



Spring Web Services provides a great way to perform web service client integration tests. However, the example in the documentation requires the client class to extend, which is rather ugly. Instead, I prefer to have WebServiceTemplate injected into my client class.

So, I made a slight tweak to my integration test to work this work…

Production Code To Be Tested

First, we need the following Spring Web Services dependency:


Let’s assume the client class looks like this:-

public class WSClientServiceImpl implements WSClientService {

    private WebServiceTemplate webServiceTemplate;

    public WSClientServiceImpl(WebServiceTemplate webServiceTemplate) {
        this.webServiceTemplate = webServiceTemplate;

    public String login(String username, String password) {
        Login login = new ObjectFactory().createLogin();

        SoapActionCallback callback = new SoapActionCallback("http://webservice/login");
        Object object = webServiceTemplate.marshalSendAndReceive(login, callback);

        JAXBElement<string> jaxbElement = (JAXBElement<string>) object;
        String sessionId = jaxbElement.getValue();

        return sessionId;

The WebServiceTemplate bean is configured like this:-

<beans ...="">
    <context:component-scan base-package="com.choonchernlim.epicapp">

    <bean id="marshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
        <property name="contextPaths">

    <bean id="webServiceTemplate" class="">
        <property name="marshaller" ref="marshaller">
        <property name="unmarshaller" ref="marshaller">
        <property name="defaultUri" value="http://webservice">

Constructing Integration Test

Make sure we have the Spring Web Services Test framework dependency:-


The integration test looks similar to the example in the Spring documentation, however, I injected WebServiceTemplate instead:-

public class WSClientServiceImplTest {

    private WSClientService wsClientService;

    private WebServiceTemplate webServiceTemplate;

    private MockWebServiceServer mockServer;

    public void createServer() throws Exception {
        mockServer = MockWebServiceServer.createServer(webServiceTemplate);

    public void testLogin() {
        Source requestPayload = new StringSource(
                "<ns2:login xmlns:ns2="http://webservice/login">" +
                "  <ns2:username>user</ns2:username>" +
                "  <ns2:password>password</ns2:password>" +

        Source responsePayload = new StringSource(
                "<sessionid xmlns="http://webservice/login">12345</sessionid>"


        String sessionId = wsClientService.login("user", "password");

        assertThat(sessionId, is("12345"));



One response to “Spring Web Services: Client Integration Testing with MockWebServiceServer”

  1. Dan Avatar

    I’m getting java.lang.AssertionError: Further connection(s) expected. Any idea if I’m missing anything?

Leave a Reply

%d bloggers like this: