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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
import org.springframework.ws.client.core.support.WebServiceGatewaySupport; public class BookingWebserviceClient extends WebServiceGatewaySupport{ @Autowired private WebserviceTemplate webserviceTemplate; public void createBookingRequest(){ BookingRequest bookingRequest = …;//Get the object from some factory BookingResponse bookingResponse = (BookingResponse) getWebServiceTemplate().marshalSendAndReceive(bookingRequest); } } |
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
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 64 65 66 67 |
import javax.xml.transform.Source; import org.springframework.beans.factory.annotation.Autowired; 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.client.MockWebServiceServer; import static org.springframework.ws.test.client.RequestMatchers.*; import static org.springframework.ws.test.client.ResponseCreators.*; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration("booking-integration-test.xml") public class BookingWebserviceClientIntegrationTest { @Autowired private BookingWebserviceClient client; private MockWebServiceServer mockServer; @Before public void createServer() throws Exception { mockServer = MockWebServiceServer.createServer(client); } @Test public void bookingWebserviceClient() throws Exception { //Prepare request payload Source requestPayload = new StringSource(“…”); //Prepare response payload Source responsePayload = new StringSource(“…”); mockServer.expect(payload(requestPayload)). andRespond(withPayload(responsePayload)); client.createBookingRequest (); //put appropriate assertions as required mockServer.verify(); } } |
Page Visitors: 10808

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