Class MockWebServiceServer
java.lang.Object
org.springframework.ws.test.client.MockWebServiceServer
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:
- Create a
MockWebServiceServer
instance by callingcreateServer(WebServiceTemplate)
,createServer(WebServiceGatewaySupport)
, orcreateServer(ApplicationContext)
. - Set up request expectations by calling
expect(RequestMatcher)
, possibly by using the defaultRequestMatcher
implementations provided inRequestMatchers
(which can be statically imported). Multiple expectations can be set up by chainingResponseActions.andExpect(RequestMatcher)
calls. - Create an appropriate response message by calling
andRespond(ResponseCreator)
, possibly by using the defaultResponseCreator
implementations provided inResponseCreators
(which can be statically imported). - Use the
WebServiceTemplate
as normal, either directly of through client code. - Call
verify()
.
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
- Author:
- Arjen Poutsma, Lukas Krecan, Greg Turnquist
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic MockWebServiceServer
createServer
(org.springframework.context.ApplicationContext applicationContext) Creates aMockWebServiceServer
instance based on the givenApplicationContext
.static MockWebServiceServer
createServer
(WebServiceGatewaySupport gatewaySupport) Creates aMockWebServiceServer
instance based on the givenWebServiceGatewaySupport
.static MockWebServiceServer
createServer
(WebServiceTemplate webServiceTemplate) Creates aMockWebServiceServer
instance based on the givenWebServiceTemplate
.expect
(RequestMatcher requestMatcher) Records an expectation specified by the givenRequestMatcher
.void
reset()
Reset theMockWebServiceMessageSender
's expectations.void
verify()
Verifies that all of theMockWebServiceMessageSender
's expectations were met.
-
Constructor Details
-
MockWebServiceServer
-
-
Method Details
-
createServer
Creates aMockWebServiceServer
instance based on the givenWebServiceTemplate
.- Parameters:
webServiceTemplate
- the web service template- Returns:
- the created server
-
createServer
Creates aMockWebServiceServer
instance based on the givenWebServiceGatewaySupport
.- Parameters:
gatewaySupport
- the client class- Returns:
- the created server
-
createServer
public static MockWebServiceServer createServer(org.springframework.context.ApplicationContext applicationContext) Creates aMockWebServiceServer
instance based on the givenApplicationContext
.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 aWebServiceGatewaySupport
, 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 aWebServiceTemplate
nor aWebServiceGatewaySupport
.
-
expect
Records an expectation specified by the givenRequestMatcher
. Returns aResponseActions
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 theMockWebServiceMessageSender
's expectations were met.- Throws:
AssertionError
- in case of unmet expectations
-
reset
public void reset()Reset theMockWebServiceMessageSender
's expectations.
-