クラス MockWebServiceClient

java.lang.ObjectSE
org.springframework.ws.test.server.MockWebServiceClient

public class MockWebServiceClient extends ObjectSE
サーバー側の Web サービステストのメインエントリポイント。通常、リクエストメッセージを作成し、レスポンスメッセージに関する期待値を設定することにより、MessageDispatcher(エンドポイント、マッピングなどを含む)をテストするために使用されます。

このクラスの一般的な使用箇所は次のとおりです。

  1. createClient(ApplicationContext) または createClient(WebServiceMessageReceiver, WebServiceMessageFactory) を使用して MockWebServiceClient インスタンスを作成します
  2. sendRequest(RequestCreator) を呼び出して、おそらく RequestCreators で提供されているデフォルトの RequestCreator 実装(静的にインポート可能)を使用して、リクエストメッセージを送信します。
  3. andExpect(ResponseMatcher) を呼び出して、おそらく ResponseMatchers で提供されているデフォルトの ResponseMatcher 実装(静的にインポート可能)を使用して、レスポンスの期待値を設定します。andExpect() 呼び出しを連鎖させることにより、複数の期待値を設定できます。
このクラス(および関連するクラス)によって提供される「流れるような」API のため、通常、IDE でコード補完機能(つまり、ctrl-space)を使用してモックを設定できることに注意してください。

例:

 import org.junit.*;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.context.ApplicationContext;
 import org.springframework.test.context.ContextConfiguration;
 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
 import org.springframework.xml.transform.StringSource;
 import org.springframework.ws.test.server.MockWebServiceClient;
 import static org.springframework.ws.test.server.RequestCreators.*;
 import static org.springframework.ws.test.server.ResponseMatchers.*;

 @RunWith(SpringJUnit4ClassRunner.class)
 @ContextConfiguration("applicationContext.xml")
 public class MyWebServiceIntegrationTest {

         // a standard MessageDispatcherServlet application context, containing endpoints, mappings, etc.
         @Autowired
         private ApplicationContext applicationContext;

         private MockWebServiceClient mockClient;

         @Before
         public void createClient() throws Exception {
           mockClient = MockWebServiceClient.createClient(applicationContext);
         }

         // test the CustomerCountEndpoint, which is wired up in the application context above
         // and handles <customerCount/> messages
         @Test
         public void customerCountEndpoint() throws Exception {
           Source requestPayload = new StringSource(
                 "<customerCountRequest xmlns='http://springframework.org/spring-ws'>" +
                 "<customerName>John Doe</customerName>" +
                 "</customerCountRequest>");
           Source expectedResponsePayload = new StringSource(
                 "<customerCountResponse xmlns='http://springframework.org/spring-ws'>" +
                 "<customerCount>42</customerCount>" +
                 "</customerCountResponse>");

           mockClient.sendRequest(withPayload(requestPayload)).andExpect(payload(expectedResponsePayload));
         }
 }
 
導入:
2.0
作成者:
Arjen Poutsma, Lukas Krecan
  • メソッドの詳細

    • createClient

      public static MockWebServiceClient createClient(WebServiceMessageReceiver messageReceiver, WebServiceMessageFactory messageFactory)
      指定された WebServiceMessageReceiver および WebServiceMessageFactory に基づいて MockWebServiceClient インスタンスを作成します。
      パラメーター:
      messageReceiver - メッセージ受信者、通常は SoapMessageDispatcher
      messageFactory - メッセージファクトリ
      戻り値:
      作成されたクライアント
    • createClient

      public static MockWebServiceClient createClient(org.springframework.context.ApplicationContext applicationContext)
      指定された ApplicationContext に基づいて MockWebServiceClient インスタンスを作成します。このファクトリメソッドは、標準の MessageDispatcherServlet と同じように機能します。あれは:
      • WebServiceMessageReceiver が特定のアプリケーションコンテキストで構成されている場合、それを使用します。メッセージレシーバーが構成されていない場合は、デフォルトの SoapMessageDispatcher が作成されます。
      • WebServiceMessageFactory が特定のアプリケーションコンテキストで構成されている場合、それを使用します。メッセージファクトリが設定されていない場合は、デフォルトの SaajSoapMessageFactory が作成されます。
      パラメーター:
      applicationContext - クライアントのベースとなるアプリケーションコンテキスト
      戻り値:
      作成されたクライアント
    • sendRequest

      public ResponseActions sendRequest(RequestCreator requestCreator)
      指定された RequestCreator を使用してリクエストメッセージを送信します。通常、RequestCreators によって提供されるデフォルトのリクエストクリエーターを使用して呼び出されます。
      パラメーター:
      requestCreator - リクエスト作成者
      戻り値:
      レスポンスアクション
      関連事項: