Spring Book – Chapter 16 – Web Services – Spring WS

Testing

Spring WS supports out-of-container integration testing. This doesn’t require stubbing/mocking of the various endpoint parameters as compared to Spring bean testing. It contains functionality to test both clients (i.e. classes that use the WebserviceTemplate), and servers (i.e. @Endpoints).

Client-side Integration Testing

The core class for doing client-side integration testing is the org.springframework.ws.test.client.MockWebServiceServer. The WebserviceTemplate connects to the mock server, sends request message and then verifies against the registered expectations. If the expectations are met, the mock server prepares a response message and then sends it back to the template.

MockWebServiceServer makes it possible to create fake server that validates request and sends response. It tests the full WebserviceTemplate configuration.

The various steps involved in using MockWebServiceServer can be summarized as below in bullet points:

  • Create an instance of MockWebServiceServer using one of its factory methods.
  • Now setup request expectation(s) by calling and “expect()” method with an org.springframework.ws.test.client.RequestMatcher callback.
  • You can now repeat the previous step with multiple assertions as required.
  • Now create a response using “andRespond()” method with a org.springframework.ws.test.client.ResponseCreator callback.
  • Now use the WebseviceTemplate as normal, either directly or through the client code.
  • Verify expectations using MockWebServiceServer.verify() method to make sure that all expectations have been met.

Listing 16-20 below shows the web service client written in a different way as we did in earlier section.

Listing 16-20. Web service client by extending WebServiceGatewauSupport class

Listing 16-21 shows a complete test class which works you through the various steps summarized as above and gives you a full example in action.

Listing 16-21. Client side integration test case for the created web service

Page Visitors: 8452

The following two tabs change content below.
Tomcy John

Tomcy John

Blogger & Author at javacodebook
He is an Enterprise Java Specialist holding a degree in Engineering (B-Tech) with over 10 years of experience in several industries. He's currently working as Principal Architect at Emirates Group IT since 2005. Prior to this he has worked with Oracle Corporation and Ernst & Young. His main specialization is on various web technologies and acts as chief mentor and Architect to facilitate incorporating Spring as Corporate Standard in the organization.

Leave a Reply

Your email address will not be published. Required fields are marked *