SWS-651 - Fishined client-side docs
This commit is contained in:
@@ -510,6 +510,15 @@ public void marshalWithSoapActionHeader(final Source s) {
|
||||
to guide you through the process of setting up the mock server.
|
||||
</para>
|
||||
</note>
|
||||
<note>
|
||||
<para>
|
||||
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 <xref linkend="logging"/> for more information.
|
||||
</para>
|
||||
</note>
|
||||
<para>
|
||||
Consider, for example, this Web service client class:
|
||||
</para>
|
||||
@@ -664,10 +673,14 @@ public class CustomerClientIntegrationTest {
|
||||
<para>
|
||||
We define expectations by calling <methodname>expect()</methodname> with a
|
||||
<methodname>payload()</methodname> <interfacename>RequestMatcher</interfacename> provided
|
||||
by the statically imported <classname>RequestMatchers</classname>.
|
||||
by the statically imported <classname>RequestMatchers</classname> (see <xref
|
||||
linkend="client-test-request-matcher"/>).
|
||||
</para>
|
||||
<para>
|
||||
We also set up a response by calling <methodname>andRespond()</methodname> with a
|
||||
<methodname>withPayload()</methodname> <interfacename>ResponseCreator</interfacename>
|
||||
provided by the statically imported <classname>ResponseCreators</classname>.
|
||||
provided by the statically imported <classname>ResponseCreators</classname> (see
|
||||
<xref linkend="client-test-response-creator"/>).
|
||||
</para>
|
||||
<para>
|
||||
This part of the test might look a bit confusing, but the Code Completion features of your
|
||||
@@ -698,7 +711,158 @@ public class CustomerClientIntegrationTest {
|
||||
</callout>
|
||||
</calloutlist>
|
||||
</programlistingco>
|
||||
</section>
|
||||
<section id="client-test-request-matcher">
|
||||
<title><interfacename>RequestMatcher</interfacename> and <classname>RequestMatchers</classname></title>
|
||||
<para>
|
||||
To verify whether the request message meets certain expectations, the
|
||||
<classname>MockWebServiceServer</classname> uses the <interfacename>RequestMatcher</interfacename>
|
||||
strategy interface.
|
||||
The contract defined by this interface is quite simple:
|
||||
</para>
|
||||
<programlisting><![CDATA[public interface RequestMatcher {
|
||||
|
||||
void match(URI uri,
|
||||
WebServiceMessage request)
|
||||
throws IOException,
|
||||
AssertionError;
|
||||
|
||||
}]]></programlisting>
|
||||
<para>
|
||||
You can write your own implementations of this interface, throwing
|
||||
<classname>AssertionError</classname>s when the message does not meet your expectations, but you
|
||||
certainly do not have to.
|
||||
The <classname>RequestMatchers</classname> class provides standard
|
||||
<interfacename>RequestMatcher</interfacename> implementations for you to use in your tests.
|
||||
You will typically statically import this class.
|
||||
</para>
|
||||
<para>
|
||||
The <classname>RequestMatchers</classname> class provides the following request matchers:
|
||||
<informaltable>
|
||||
<tgroup cols="2">
|
||||
<thead>
|
||||
<row>
|
||||
<entry><classname>RequestMatchers</classname> method</entry>
|
||||
<entry>Description</entry>
|
||||
</row>
|
||||
</thead>
|
||||
<tbody>
|
||||
<row>
|
||||
<entry><methodname>anything()</methodname></entry>
|
||||
<entry>Expects any sort of request.</entry>
|
||||
</row>
|
||||
<row>
|
||||
<entry><methodname>payload()</methodname></entry>
|
||||
<entry>Expects a given request payload.</entry>
|
||||
</row>
|
||||
<row>
|
||||
<entry><methodname>validPayload()</methodname></entry>
|
||||
<entry>Expects the request payload to validate against given XSD schema(s).</entry>
|
||||
</row>
|
||||
<row>
|
||||
<entry><methodname>xpath()</methodname></entry>
|
||||
<entry>
|
||||
Expects a given XPath expression to exist, not exist, or evaluate to a given
|
||||
value.
|
||||
</entry>
|
||||
</row>
|
||||
<row>
|
||||
<entry><methodname>soapHeader()</methodname></entry>
|
||||
<entry>Expects a given SOAP header to exist in the request message.</entry>
|
||||
</row>
|
||||
<row>
|
||||
<entry><methodname>connectionTo()</methodname></entry>
|
||||
<entry>Expects a connection to the given URL.</entry>
|
||||
</row>
|
||||
</tbody>
|
||||
</tgroup>
|
||||
</informaltable>
|
||||
You can set up multiple request expectations by chaining <methodname>andExpect()</methodname> calls,
|
||||
like so:
|
||||
<programlisting>mockServer.expect(connectionTo("http://example.com")).
|
||||
andExpect(payload(expectedRequestPayload)).
|
||||
andExpect(validPayload(schemaResource)).
|
||||
andRespond(...);
|
||||
</programlisting>
|
||||
</para>
|
||||
<para>
|
||||
For more information on the request matchers provided by <classname>RequestMatchers</classname>,
|
||||
refer to the class level Javadoc.
|
||||
</para>
|
||||
</section>
|
||||
<section id="client-test-response-creator">
|
||||
<title><interfacename>ResponseCreator</interfacename> and <classname>ResponseCreators</classname></title>
|
||||
<para>
|
||||
When the request message has been verified and meets the defined expectations, the
|
||||
<classname>MockWebServiceServer</classname> will create a response message for the
|
||||
<classname>WebServiceTemplate</classname> to consume.
|
||||
The server uses the <interfacename>ResponseCreator</interfacename>
|
||||
strategy interface for this purpose:
|
||||
</para>
|
||||
<programlisting><![CDATA[public interface ResponseCreator {
|
||||
|
||||
WebServiceMessage createResponse(URI uri,
|
||||
WebServiceMessage request,
|
||||
WebServiceMessageFactory messageFactory)
|
||||
throws IOException;
|
||||
|
||||
}]]></programlisting>
|
||||
<para>
|
||||
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
|
||||
<classname>ResponseCreators</classname> class provides standard
|
||||
<interfacename>ResponseCreator</interfacename> implementations for you to use in your tests.
|
||||
You will typically statically import this class.
|
||||
</para>
|
||||
<para>
|
||||
The <classname>ResponseCreators</classname> class provides the following responses:
|
||||
<informaltable>
|
||||
<tgroup cols="2">
|
||||
<thead>
|
||||
<row>
|
||||
<entry><classname>ResponseCreators</classname> method</entry>
|
||||
<entry>Description</entry>
|
||||
</row>
|
||||
</thead>
|
||||
<tbody>
|
||||
<row>
|
||||
<entry><methodname>withPayload()</methodname></entry>
|
||||
<entry>Creates a response message with a given payload.</entry>
|
||||
</row>
|
||||
<row>
|
||||
<entry><methodname>withError()</methodname></entry>
|
||||
<entry>
|
||||
Creates an error in the response connection.
|
||||
This method gives you the opportunity to test your error handling.
|
||||
</entry>
|
||||
</row>
|
||||
<row>
|
||||
<entry><methodname>withException()</methodname></entry>
|
||||
<entry>
|
||||
Throws an exception when reading from the response connection.
|
||||
This method gives you the opportunity to test your exception handling.
|
||||
</entry>
|
||||
</row>
|
||||
<row>
|
||||
<entry>
|
||||
<methodname>withMustUnderstandFault()</methodname>,
|
||||
<methodname>withClientOrSenderFault()</methodname>,
|
||||
<methodname>withServerOrReceiverFault()</methodname>, and
|
||||
<methodname>withVersionMismatchFault()</methodname>
|
||||
</entry>
|
||||
<entry>
|
||||
Creates a response message with a given SOAP fault.
|
||||
This method gives you the opportunity to test your Fault handling.
|
||||
</entry>
|
||||
</row>
|
||||
</tbody>
|
||||
</tgroup>
|
||||
</informaltable>
|
||||
</para>
|
||||
<para>
|
||||
For more information on the request matchers provided by <classname>RequestMatchers</classname>,
|
||||
refer to the class level Javadoc.
|
||||
</para>
|
||||
</section>
|
||||
</section>
|
||||
</chapter>
|
||||
@@ -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();
|
||||
}
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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">
|
||||
|
||||
<bean id="client" class="org.springframework.ws.test.client.integration.CustomerClient">
|
||||
<property name="webServiceTemplate" ref="webServiceTemplate"/>
|
||||
</bean>
|
||||
|
||||
<bean id="webServiceTemplate" class="org.springframework.ws.client.core.WebServiceTemplate">
|
||||
<property name="marshaller" ref="marshaller"/>
|
||||
<property name="unmarshaller" ref="marshaller"/>
|
||||
|
||||
Reference in New Issue
Block a user