Class MockWebServiceClient
java.lang.Object
org.springframework.ws.test.server.MockWebServiceClient
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:
- Create a
MockWebServiceClientinstance by usingcreateClient(ApplicationContext)orcreateClient(WebServiceMessageReceiver, WebServiceMessageFactory) - Send request messages by calling
sendRequest(RequestCreator), possibly by using the defaultRequestCreatorimplementations provided inRequestCreators(which can be statically imported). - Set up response expectations by calling
andExpect(ResponseMatcher), possibly by using the defaultResponseMatcherimplementations provided inResponseMatchers(which can be statically imported). Multiple expectations can be set up by chainingandExpect()calls.
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 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("applicationContext.xml")
public class MyWebServiceIntegrationTest {
// a standard MessageDispatcherServlet application context, containing endpoints, mappings, etc.
@Autowired
private ApplicationContext applicationContext;
private MockWebServiceClient mockClient;
@Before
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
@Test
public void customerCountEndpoint() throws Exception {
Source requestPayload = new StringSource(
"<customerCountRequest xmlns='http://springframework.org/spring-ws'>" +
"<customerName>John Doe</customerName>" +
"</customerCountRequest>");
Source expectedResponsePayload = new StringSource(
"<customerCountResponse xmlns='http://springframework.org/spring-ws'>" +
"<customerCount>42</customerCount>" +
"</customerCountResponse>");
mockClient.sendRequest(withPayload(requestPayload)).andExpect(payload(expectedResponsePayload));
}
}
- Since:
- 2.0
- Author:
- Arjen Poutsma, Lukas Krecan
-
Method Summary
Modifier and TypeMethodDescriptionstatic MockWebServiceClientcreateClient(org.springframework.context.ApplicationContext applicationContext) Creates aMockWebServiceClientinstance based on the givenApplicationContext.static MockWebServiceClientcreateClient(WebServiceMessageReceiver messageReceiver, WebServiceMessageFactory messageFactory) Creates aMockWebServiceClientinstance based on the givenWebServiceMessageReceiverandWebServiceMessageFactory.sendRequest(RequestCreator requestCreator) Sends a request message by using the givenRequestCreator.
-
Method Details
-
createClient
public static MockWebServiceClient createClient(WebServiceMessageReceiver messageReceiver, WebServiceMessageFactory messageFactory) Creates aMockWebServiceClientinstance based on the givenWebServiceMessageReceiverandWebServiceMessageFactory.- Parameters:
messageReceiver- the message receiver, typically aSoapMessageDispatchermessageFactory- the message factory- Returns:
- the created client
-
createClient
public static MockWebServiceClient createClient(org.springframework.context.ApplicationContext applicationContext) Creates aMockWebServiceClientinstance based on the givenApplicationContext. This factory method works in a similar fashion as the standardMessageDispatcherServlet. That is:- If a
WebServiceMessageReceiveris configured in the given application context, it will use that. If no message receiver is configured, it will create a defaultSoapMessageDispatcher. - If a
WebServiceMessageFactoryis configured in the given application context, it will use that. If no message factory is configured, it will create a defaultSaajSoapMessageFactory.
- Parameters:
applicationContext- the application context to base the client on- Returns:
- the created client
- If a
-
sendRequest
Sends a request message by using the givenRequestCreator. Typically called by using the default request creators provided byRequestCreators.- Parameters:
requestCreator- the request creator- Returns:
- the response actions
- See Also:
-