From 7297276b49267bd0e558d1692fccce96d9a61dd8 Mon Sep 17 00:00:00 2001 From: Arjen Poutsma Date: Fri, 30 Jul 2010 10:28:16 +0000 Subject: [PATCH] SWS-631 - documentation --- .../mock/client/PayloadResponseCallback.java | 2 +- .../ws/mock/client/WebServiceMock.java | 75 +++++++++++++++++++ .../ws/mock/client/package-info.java | 21 ++++++ 3 files changed, 97 insertions(+), 1 deletion(-) create mode 100644 test/src/main/java/org/springframework/ws/mock/client/package-info.java diff --git a/test/src/main/java/org/springframework/ws/mock/client/PayloadResponseCallback.java b/test/src/main/java/org/springframework/ws/mock/client/PayloadResponseCallback.java index 5ec9351d..20efe588 100644 --- a/test/src/main/java/org/springframework/ws/mock/client/PayloadResponseCallback.java +++ b/test/src/main/java/org/springframework/ws/mock/client/PayloadResponseCallback.java @@ -38,7 +38,7 @@ class PayloadResponseCallback extends AbstractResponseCreator } @Override - public void doWithResponse(URI uri, WebServiceMessage request, WebServiceMessage response) throws IOException { + protected void doWithResponse(URI uri, WebServiceMessage request, WebServiceMessage response) throws IOException { try { transform(payload, response.getPayloadResult()); } diff --git a/test/src/main/java/org/springframework/ws/mock/client/WebServiceMock.java b/test/src/main/java/org/springframework/ws/mock/client/WebServiceMock.java index ecfd8f8d..e6156636 100644 --- a/test/src/main/java/org/springframework/ws/mock/client/WebServiceMock.java +++ b/test/src/main/java/org/springframework/ws/mock/client/WebServiceMock.java @@ -32,12 +32,87 @@ import org.springframework.xml.validation.XmlValidator; import org.springframework.xml.validation.XmlValidatorFactory; /** + * Main entry point for client-side Web service testing. Typically used to mock a {@link + * WebServiceTemplate}, set up expectations on request messages, and create response messages. + *

+ * The typical usage of this mock is similar to any other mocking library (such as EasyMock), that is: + *

    + *
  1. Statically import {@link org.springframework.ws.mock.client.WebServiceMock + * org.springframework.ws.mock.client.WebServiceMock.*}. + *
  2. Use the {@link #mockWebServiceTemplate(WebServiceTemplate)} method to mock a web service template. Typically, + * this template is configured as a Spring bean, either explicitly or as a property of a class that extends + * {@link org.springframework.ws.client.core.support.WebServiceGatewaySupport WebServiceGatewaySupport}.
  3. + *
  4. Set up expectations on the outgoing request message by calling {@link #expect(RequestMatcher)} and + * {@link #payload(Source)}, {@link #connectionTo(String)}, {@link #xpath(String)}, or any of the other + * {@linkplain RequestMatcher request matcher} methods. + * Multiple expectations can be set up by calling + * {@link ResponseActions#andExpect(RequestMatcher) andExpect(RequestMatcher)}.
  5. + *
  6. Indicate the desired response actions by calling + * {@link ResponseActions#andRespond(ResponseCreator) andRespond(ResponseCreator)}. + * See {@link #withPayload(Source)}, {@link #withError(String)}, + * {@link #withClientOrSenderFault(String, Locale)}, or any of the other {@linkplain ResponseCreator response creator} + * methods.
  7. + *
  8. Use the {@code WebServiceTemplate} as normal, either directly of through client code. + *
  9. Call {@link #verifyConnections()}. + *
+ * Note that because of the 'fluent' API offered by this class, you can typically use the Code Completion features (i.e. + * ctrl-space) in your IDE to set up the mocks. + *

+ * For example: + *

+ * import org.junit.Before;
+ * import org.junit.Test;
+ * import org.junit.runner.RunWith;
+ * 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 static org.springframework.ws.mock.client.WebServiceMock.*;
+ *
+ * 
+ * @RunWith(SpringJUnit4ClassRunner.class)
+ * @ContextConfiguration("applicationContext.xml")
+ * public class IntegrationTest {
+ *
+ *   // AirlineClient extends WebServiceGatewaySupport, and is configured in applicationContext.xml
+ *   @Autowired
+ *   private MyWebServiceClient client;
+ *
+ *   @Before
+ *   public void setUpMocks() throws Exception {
+ *     mockWebServiceTemplate(client.getWebServiceTemplate());
+ *   }
+ *
+ *   @Test
+ *   public void getCustomerCount() throws Exception {
+ *     Source requestPayload =
+ *       new StringSource("<customerCountRequest xmlns='http://springframework.org/spring-ws/test' />";
+ *     Source responsePayload = new StringSource("<customerCountResponse xmlns='http://springframework.org/spring-wstest'>" +
+ *       "<customerCount>10</customerCount>" +
+ *       "</customerCountResponse>");
+ *
+ *     expect(payload(requestPayload)).andRespond(withPayload(responsePayload));
+ *
+ *     // client.getCustomerCount() uses the WebServiceTemplate
+ *     int customerCount = client.getCustomerCount();
+ *     assertEquals(10, response.getCustomerCount());
+ *
+ *     verifyConnections();
+ *   }
+ * }
+ * 
+ * * @author Arjen Poutsma * @author Lukas Krecan * @since 2.0 */ public abstract class WebServiceMock { + /** + * Mocks the given {@link WebServiceTemplate}. Typically done in a setup method of the test class. + * + * @param webServiceTemplate the template to mock. + */ public static void mockWebServiceTemplate(WebServiceTemplate webServiceTemplate) { Assert.notNull(webServiceTemplate, "'webServiceTemplate' must not be null"); diff --git a/test/src/main/java/org/springframework/ws/mock/client/package-info.java b/test/src/main/java/org/springframework/ws/mock/client/package-info.java new file mode 100644 index 00000000..80f12ce7 --- /dev/null +++ b/test/src/main/java/org/springframework/ws/mock/client/package-info.java @@ -0,0 +1,21 @@ +/* + * 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. + */ + +/** + * Provides a testing framework for client-side Web service testing. This package contains the + * {@link org.springframework.ws.mock.client.WebServiceMock}, and various related test interfaces. + */ +package org.springframework.ws.mock.client;