From 01bcd2661a113ad2ea31758724a85333761ca638 Mon Sep 17 00:00:00 2001 From: Arjen Poutsma Date: Tue, 9 Nov 2010 12:09:59 +0000 Subject: [PATCH] SWS-651 - Fishined client-side docs --- src/docbkx/client.xml | 168 +++++++++++++++++- .../integration/ClientIntegrationTest.java | 14 +- .../client/integration/CustomerClient.java | 34 ++++ .../client/integration/integration-test.xml | 4 + 4 files changed, 208 insertions(+), 12 deletions(-) create mode 100644 test/src/test/java/org/springframework/ws/test/client/integration/CustomerClient.java diff --git a/src/docbkx/client.xml b/src/docbkx/client.xml index 079179d5..d793549e 100644 --- a/src/docbkx/client.xml +++ b/src/docbkx/client.xml @@ -510,6 +510,15 @@ public void marshalWithSoapActionHeader(final Source s) { to guide you through the process of setting up the mock server. + + + Also note that you rely on the standard logging features available in Spring Web Services in your + unit tests. + Sometimes it might be useful to inspect the request or response message to find out why a + particular tests failed. + See for more information. + + Consider, for example, this Web service client class: @@ -664,10 +673,14 @@ public class CustomerClientIntegrationTest { We define expectations by calling expect() with a payload() RequestMatcher provided - by the statically imported RequestMatchers. + by the statically imported RequestMatchers (see ). + + We also set up a response by calling andRespond() with a withPayload() ResponseCreator - provided by the statically imported ResponseCreators. + provided by the statically imported ResponseCreators (see + ). This part of the test might look a bit confusing, but the Code Completion features of your @@ -698,7 +711,158 @@ public class CustomerClientIntegrationTest { + +
+ <interfacename>RequestMatcher</interfacename> and <classname>RequestMatchers</classname> + + To verify whether the request message meets certain expectations, the + MockWebServiceServer uses the RequestMatcher + strategy interface. + The contract defined by this interface is quite simple: + + + + You can write your own implementations of this interface, throwing + AssertionErrors when the message does not meet your expectations, but you + certainly do not have to. + The RequestMatchers class provides standard + RequestMatcher implementations for you to use in your tests. + You will typically statically import this class. + + + The RequestMatchers class provides the following request matchers: + + + + + RequestMatchers method + Description + + + + + anything() + Expects any sort of request. + + + payload() + Expects a given request payload. + + + validPayload() + Expects the request payload to validate against given XSD schema(s). + + + xpath() + + Expects a given XPath expression to exist, not exist, or evaluate to a given + value. + + + + soapHeader() + Expects a given SOAP header to exist in the request message. + + + connectionTo() + Expects a connection to the given URL. + + + + + You can set up multiple request expectations by chaining andExpect() calls, + like so: + mockServer.expect(connectionTo("http://example.com")). + andExpect(payload(expectedRequestPayload)). + andExpect(validPayload(schemaResource)). + andRespond(...); + + + + For more information on the request matchers provided by RequestMatchers, + refer to the class level Javadoc. + +
+
+ <interfacename>ResponseCreator</interfacename> and <classname>ResponseCreators</classname> + + When the request message has been verified and meets the defined expectations, the + MockWebServiceServer will create a response message for the + WebServiceTemplate to consume. + The server uses the ResponseCreator + strategy interface for this purpose: + + + + Once again you can write your own implementations of this interface, creating a response message + by using the message factory, but you certainly do not have to, as the + ResponseCreators class provides standard + ResponseCreator implementations for you to use in your tests. + You will typically statically import this class. + + + The ResponseCreators class provides the following responses: + + + + + ResponseCreators method + Description + + + + + withPayload() + Creates a response message with a given payload. + + + withError() + + Creates an error in the response connection. + This method gives you the opportunity to test your error handling. + + + + withException() + + Throws an exception when reading from the response connection. + This method gives you the opportunity to test your exception handling. + + + + + withMustUnderstandFault(), + withClientOrSenderFault(), + withServerOrReceiverFault(), and + withVersionMismatchFault() + + + Creates a response message with a given SOAP fault. + This method gives you the opportunity to test your Fault handling. + + + + + + + + For more information on the request matchers provided by RequestMatchers, + refer to the class level Javadoc. +
\ No newline at end of file diff --git a/test/src/test/java/org/springframework/ws/test/client/integration/ClientIntegrationTest.java b/test/src/test/java/org/springframework/ws/test/client/integration/ClientIntegrationTest.java index d23abdcd..cbeab0d0 100644 --- a/test/src/test/java/org/springframework/ws/test/client/integration/ClientIntegrationTest.java +++ b/test/src/test/java/org/springframework/ws/test/client/integration/ClientIntegrationTest.java @@ -21,10 +21,7 @@ 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.ws.client.core.WebServiceTemplate; import org.springframework.ws.test.client.MockWebServiceServer; -import org.springframework.ws.test.integration.CustomerCountRequest; -import org.springframework.ws.test.integration.CustomerCountResponse; import org.springframework.xml.transform.StringSource; import org.junit.Before; @@ -46,13 +43,13 @@ import static org.springframework.ws.test.client.ResponseCreators.withPayload; public class ClientIntegrationTest { @Autowired - private WebServiceTemplate webServiceTemplate; + private CustomerClient client; private MockWebServiceServer mockServer; @Before public void createServer() throws Exception { - mockServer = MockWebServiceServer.createServer(webServiceTemplate); + mockServer = MockWebServiceServer.createServer(client); } @Test @@ -66,11 +63,8 @@ public class ClientIntegrationTest { mockServer.expect(payload(expectedRequestPayload)).andRespond(withPayload(responsePayload)); - CustomerCountRequest request = new CustomerCountRequest(); - request.setCustomerName("John Doe"); - - CustomerCountResponse response = (CustomerCountResponse) webServiceTemplate.marshalSendAndReceive(request); - assertEquals(10, response.getCustomerCount()); + int result = client.getCustomerCount(); + assertEquals(10, result); mockServer.verify(); } diff --git a/test/src/test/java/org/springframework/ws/test/client/integration/CustomerClient.java b/test/src/test/java/org/springframework/ws/test/client/integration/CustomerClient.java new file mode 100644 index 00000000..f52e0358 --- /dev/null +++ b/test/src/test/java/org/springframework/ws/test/client/integration/CustomerClient.java @@ -0,0 +1,34 @@ +/* + * Copyright 2005-2010 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.ws.test.client.integration; + +import org.springframework.ws.client.core.support.WebServiceGatewaySupport; +import org.springframework.ws.test.integration.CustomerCountRequest; +import org.springframework.ws.test.integration.CustomerCountResponse; + + +public class CustomerClient extends WebServiceGatewaySupport { + + public int getCustomerCount() { + CustomerCountRequest request = new CustomerCountRequest(); + request.setCustomerName("John Doe"); + + CustomerCountResponse response = (CustomerCountResponse) getWebServiceTemplate().marshalSendAndReceive(request); + return response.getCustomerCount(); + } + +} diff --git a/test/src/test/resources/org/springframework/ws/test/client/integration/integration-test.xml b/test/src/test/resources/org/springframework/ws/test/client/integration/integration-test.xml index 9b5bbe65..a24a8d9b 100644 --- a/test/src/test/resources/org/springframework/ws/test/client/integration/integration-test.xml +++ b/test/src/test/resources/org/springframework/ws/test/client/integration/integration-test.xml @@ -3,6 +3,10 @@ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> + + + +