Class MockWebServiceClient


public class MockWebServiceClient extends Object
Main entry point for server-side Web service testing. Typically used to test a MessageDispatcher (including its endpoints, mappings, etc) by creating request messages, and setting up expectations about response messages.

The typical usage of this class is:

  1. Create a MockWebServiceClient instance by using createClient(ApplicationContext) or createClient(WebServiceMessageReceiver, WebServiceMessageFactory)
  2. Send request messages by calling sendRequest(RequestCreator), possibly by using the default RequestCreator implementations provided in RequestCreators (which can be statically imported).
  3. Set up response expectations by calling andExpect(ResponseMatcher), possibly by using the default ResponseMatcher implementations provided in ResponseMatchers (which can be statically imported). Multiple expectations can be set up by chaining andExpect() calls.
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.context.ApplicationContext;
 import org.springframework.test.context.ContextConfiguration;
 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
 import org.springframework.xml.transform.StringSource;
 import static*;
 import static*;

 public class MyWebServiceIntegrationTest {

         // a standard MessageDispatcherServlet application context, containing endpoints, mappings, etc.
         private ApplicationContext applicationContext;

         private MockWebServiceClient mockClient;

         public void createClient() throws Exception {
           mockClient = MockWebServiceClient.createClient(applicationContext);

         // test the CustomerCountEndpoint, which is wired up in the application context above
         // and handles <customerCount/> messages
         public void customerCountEndpoint() throws Exception {
           Source requestPayload = new StringSource(
                 "<customerCountRequest xmlns=''>" +
                 "<customerName>John Doe</customerName>" +
           Source expectedResponsePayload = new StringSource(
                 "<customerCountResponse xmlns=''>" +
                 "<customerCount>42</customerCount>" +

Arjen Poutsma, Lukas Krecan