|
32 | 32 | import org.springframework.xml.validation.XmlValidatorFactory; |
33 | 33 |
|
34 | 34 | /** |
| 35 | + * <strong>Main entry point for client-side Web service testing</strong>. Typically used to mock a {@link |
| 36 | + * WebServiceTemplate}, set up expectations on request messages, and create response messages. |
| 37 | + * <p/> |
| 38 | + * The typical usage of this mock is similar to any other mocking library (such as EasyMock), that is: |
| 39 | + * <ol> |
| 40 | + * <li>Statically import {@link org.springframework.ws.mock.client.WebServiceMock |
| 41 | + * org.springframework.ws.mock.client.WebServiceMock.*}. |
| 42 | + * <li>Use the {@link #mockWebServiceTemplate(WebServiceTemplate)} method to mock a web service template. Typically, |
| 43 | + * this template is configured as a Spring bean, either explicitly or as a property of a class that extends |
| 44 | + * {@link org.springframework.ws.client.core.support.WebServiceGatewaySupport WebServiceGatewaySupport}.</li> |
| 45 | + * <li>Set up expectations on the outgoing request message by calling {@link #expect(RequestMatcher)} and |
| 46 | + * {@link #payload(Source)}, {@link #connectionTo(String)}, {@link #xpath(String)}, or any of the other |
| 47 | + * {@linkplain RequestMatcher request matcher} methods. |
| 48 | + * Multiple expectations can be set up by calling |
| 49 | + * {@link ResponseActions#andExpect(RequestMatcher) andExpect(RequestMatcher)}.</li> |
| 50 | + * <li>Indicate the desired response actions by calling |
| 51 | + * {@link ResponseActions#andRespond(ResponseCreator) andRespond(ResponseCreator)}. |
| 52 | + * See {@link #withPayload(Source)}, {@link #withError(String)}, |
| 53 | + * {@link #withClientOrSenderFault(String, Locale)}, or any of the other {@linkplain ResponseCreator response creator} |
| 54 | + * methods.</li> |
| 55 | + * <li>Use the {@code WebServiceTemplate} as normal, either directly of through client code. |
| 56 | + * <li>Call {@link #verifyConnections()}. |
| 57 | + * </ol> |
| 58 | + * Note that because of the 'fluent' API offered by this class, you can typically use the Code Completion features (i.e. |
| 59 | + * ctrl-space) in your IDE to set up the mocks. |
| 60 | + * <p/> |
| 61 | + * For example: |
| 62 | + * <blockquote><pre> |
| 63 | + * import org.junit.Before; |
| 64 | + * import org.junit.Test; |
| 65 | + * import org.junit.runner.RunWith; |
| 66 | + * import org.springframework.beans.factory.annotation.Autowired; |
| 67 | + * import org.springframework.test.context.ContextConfiguration; |
| 68 | + * import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; |
| 69 | + * import org.springframework.xml.transform.StringSource; |
| 70 | + * <strong>import static org.springframework.ws.mock.client.WebServiceMock.*</strong>; |
| 71 | + * |
| 72 | + * |
| 73 | + * @RunWith(SpringJUnit4ClassRunner.class) |
| 74 | + * @ContextConfiguration("applicationContext.xml") |
| 75 | + * public class IntegrationTest { |
| 76 | + * |
| 77 | + * // AirlineClient extends WebServiceGatewaySupport, and is configured in applicationContext.xml |
| 78 | + * @Autowired |
| 79 | + * private MyWebServiceClient client; |
| 80 | + * |
| 81 | + * @Before |
| 82 | + * public void setUpMocks() throws Exception { |
| 83 | + * <strong>mockWebServiceTemplate(client.getWebServiceTemplate())</strong>; |
| 84 | + * } |
| 85 | + * |
| 86 | + * @Test |
| 87 | + * public void getCustomerCount() throws Exception { |
| 88 | + * Source requestPayload = |
| 89 | + * new StringSource("<customerCountRequest xmlns='http://springframework.org/spring-ws/test' />"; |
| 90 | + * Source responsePayload = new StringSource("<customerCountResponse xmlns='http://springframework.org/spring-wstest'>" + |
| 91 | + * "<customerCount>10</customerCount>" + |
| 92 | + * "</customerCountResponse>"); |
| 93 | + * |
| 94 | + * <strong>expect(payload(requestPayload)).andRespond(withPayload(responsePayload));</strong> |
| 95 | + * |
| 96 | + * // client.getCustomerCount() uses the WebServiceTemplate |
| 97 | + * int customerCount = client.getCustomerCount(); |
| 98 | + * assertEquals(10, response.getCustomerCount()); |
| 99 | + * |
| 100 | + * <strong>verifyConnections();</strong> |
| 101 | + * } |
| 102 | + * } |
| 103 | + * </pre></blockquote> |
| 104 | + * |
35 | 105 | * @author Arjen Poutsma |
36 | 106 | * @author Lukas Krecan |
37 | 107 | * @since 2.0 |
38 | 108 | */ |
39 | 109 | public abstract class WebServiceMock { |
40 | 110 |
|
| 111 | + /** |
| 112 | + * Mocks the given {@link WebServiceTemplate}. Typically done in a setup method of the test class. |
| 113 | + * |
| 114 | + * @param webServiceTemplate the template to mock. |
| 115 | + */ |
41 | 116 | public static void mockWebServiceTemplate(WebServiceTemplate webServiceTemplate) { |
42 | 117 | Assert.notNull(webServiceTemplate, "'webServiceTemplate' must not be null"); |
43 | 118 |
|
|
0 commit comments