Server-side Integration Testing
Similar to client-side, the core class for doing server-side integration testing is the org.springframework.ws.test.server.MockWebServiceClient. It sends request and validates response using fake clients. It also allows to fully testing the org.springframework.ws.server.MessageDispatcher class configuration in all aspects.
MockWebServiceClient class creates a request message and then sends it over to the endpoint(s) that are configured in a standard MessageDispatcherServlet application context. These endpoints will then handle the message, and create appropriate response. The client then receives this response, and verifies it against registered expectations.
The various steps involved in using MockWebServiceClient can be summarized as below in bullet points:
- Create an instance of MockWebServiceClient using one of its factory methods.
- Now send messages by calling the “sendRequest()” method with a org.springframework.ws.test.server.RequestCreator callback.
- Now setup the response expectation(s) by calling “andExpect” method with a org.springframework.ws.test.server.ResponseMatcher callback.
- You can now repeat the previous step for multiple assertions according to your application requirement.
Just so that you don’t want to turn pages back, here is Listing 16-22 which shows the same EndPoint for the booking service repeated.
Listing 16-22. Java class for configuring endpoint
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
import org.springframework.ws.server.endpoint.annotation.Endpoint; import org.springframework.ws.server.endpoint.annotation.PayloadRoot; @Endpoint public class BookingEndpoint{ @PayloadRoot(localPart = “bookingRequest”, namespace = “http://www.apress.com/loyalty”) public BookingResponse createBookingRequest(BookingRequest request){ //… Creation of booking logic BookingResponse result = new BookingResponse(); result.setResponse( “Request was: “ + request.getRequest() ); return result; } } |
Listing 16-23 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-23. Server side integration test case for the created web service
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
import javax.xml.transform.Source; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.ApplicationContext; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.xml.transform.StringSource; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.ws.test.server.MockWebServiceClient; import static org.springframework.ws.test.server.RequestCreators.*; import static org.springframework.ws.test.server.ResponseMatchers.*; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration("spring-ws-servlet.xml") public class BookingEndpointIntegrationTest { @Autowired private ApplicationContext applicationContext; private MockWebServiceClient mockClient; @Before public void createClient() { mockClient = MockWebServiceClient.createClient(applicationContext); } @Test public void bookingEndpoint() throws Exception { //Prepare request payload Source requestPayload = new StringSource(“…”); //Prepare response payload Source responsePayload = new StringSource(“…”); mockClient.sendRequest(withPayload(requestPayload)). andExpect(payload(responsePayload)); } } |
Summary
After reading this chapter, you should now have a clear idea of what a web service is along with various terminologies and concepts. You should also have a clear idea of the support provided by Spring WS in developing web service for your application.
Spring WS is a contract-first way of creating web services using XML schema. It has robust implementation using XPath which is very key in its adoption. It has easy and simple annotation based programming model as well.
Being contract-first is both its strength and weakness. It doesn’t haven any code generation and is made exclusively for and by the Spring Framework. It supports two WS-security implementations namely WSS4J and XWSS in all aspects and securing your web service is as easy as configuring your Spring beans. It supports different XML marshalling and handling technologies, making it ideal to configure according to your application requirement. It also has a capability of generating WSDL from the declared XSD as handling XSD is much easy compared to WSDL. One of the weaknesses of Spring WS is that it required programmatic processing of XML message payloads which could be development intensive to some extend.
The chapter provided a step by step approach of creating and end to end web service, which would have given you hands-on experience on creating a web service using Spring WS. Later on we covered some advanced topics like interceptors and testing capabilities which Spring WS gives you to use in your application. You should now have a clear picture of these advanced topics as well.
I hope you have enjoyed reading this Chapter to the last page and would be ready in all aspects to write your own web service in your application.
Page Visitors: 10629

Tomcy John

Latest posts by Tomcy John (see all)
- A Guide to Continuous Improvement for Architects - February 2, 2023
- Cloud-first Architecture Strategy - January 26, 2023
- Architecture Strategy and how to create One - January 24, 2023