Class MockWebServiceServer

java.lang.Object
org.springframework.ws.test.client.MockWebServiceServer

public class MockWebServiceServer extends Object
Main entry point for client-side Web service testing. Typically used to test a WebServiceTemplate, set up expectations on request messages, and create response messages.

The typical usage of this class is:

  1. Create a MockWebServiceServer instance by calling createServer(WebServiceTemplate), createServer(WebServiceGatewaySupport), or createServer(ApplicationContext).
  2. Set up request expectations by calling expect(RequestMatcher), possibly by using the default RequestMatcher implementations provided in RequestMatchers (which can be statically imported). Multiple expectations can be set up by chaining ResponseActions.andExpect(RequestMatcher) calls.
  3. Create an appropriate response message by calling andRespond(ResponseCreator), possibly by using the default ResponseCreator implementations provided in ResponseCreators (which can be statically imported).
  4. Use the WebServiceTemplate as normal, either directly of through client code.
  5. Call verify().
Note that because of the 'fluent' API offered by this class (and related classes), you can typically use the Code Completion features (i.e. ctrl-space) in your IDE to set up the mocks.

For example:


 import org.junit.*;
 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.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("applicationContext.xml")
 public class MyWebServiceClientIntegrationTest {

         // MyWebServiceClient extends WebServiceGatewaySupport, and is configured in applicationContext.xml
         @Autowired
         private MyWebServiceClient client;

         private MockWebServiceServer mockServer;

         @Before
         public void createServer() throws Exception {
           mockServer = MockWebServiceServer.createServer(client);
         }

         @Test
         public void getCustomerCount() throws Exception {
           Source expectedRequestPayload =
                 new StringSource("<customerCountRequest xmlns=\"http://springframework.org/spring-ws/test\" />");
           Source responsePayload = new StringSource("<customerCountResponse xmlns='http://springframework.org/spring-ws/test'>" +
                 "<customerCount>10</customerCount>" +
                 "</customerCountResponse>");

           mockServer.expect(payload(expectedRequestPayload)).andRespond(withPayload(responsePayload));

           // client.getCustomerCount() uses the WebServiceTemplate
           int customerCount = client.getCustomerCount();
           assertEquals(10, response.getCustomerCount());

           mockServer.verify();
         }
 }
Since:
2.0
  • Constructor Details

  • Method Details

    • createServer

      public static MockWebServiceServer createServer(org.springframework.ws.client.core.WebServiceTemplate webServiceTemplate)
      Creates a MockWebServiceServer instance based on the given WebServiceTemplate.
      Parameters:
      webServiceTemplate - the web service template
      Returns:
      the created server
    • createServer

      public static MockWebServiceServer createServer(org.springframework.ws.client.core.support.WebServiceGatewaySupport gatewaySupport)
      Creates a MockWebServiceServer instance based on the given WebServiceGatewaySupport.
      Parameters:
      gatewaySupport - the client class
      Returns:
      the created server
    • createServer

      public static MockWebServiceServer createServer(org.springframework.context.ApplicationContext applicationContext)
      Creates a MockWebServiceServer instance based on the given ApplicationContext.

      This factory method will try and find a configured WebServiceTemplate in the given application context. If no template can be found, it will try and find a WebServiceGatewaySupport, and use its configured template. If neither can be found, an exception is thrown.

      Parameters:
      applicationContext - the application context to base the client on
      Returns:
      the created server
      Throws:
      IllegalArgumentException - if the given application context contains neither a WebServiceTemplate nor a WebServiceGatewaySupport.
    • expect

      public ResponseActions expect(RequestMatcher requestMatcher)
      Records an expectation specified by the given RequestMatcher. Returns a ResponseActions object that allows for creating the response, or to set up more expectations.
      Parameters:
      requestMatcher - the request matcher expected
      Returns:
      the response actions
    • verify

      public void verify()
      Verifies that all of the MockWebServiceMessageSender's expectations were met.
      Throws:
      AssertionError - in case of unmet expectations
    • reset

      public void reset()
      Reset the MockWebServiceMessageSender's expectations.