クラス MockWebServiceServer

java.lang.ObjectSE
org.springframework.ws.test.client.MockWebServiceServer

public class MockWebServiceServer extends ObjectSE
クライアント側の Web サービステストのメインエントリポイント。通常、WebServiceTemplate のテスト、リクエストメッセージに対する期待値の設定、レスポンスメッセージの作成に使用されます。

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

  1. createServer(WebServiceTemplate)createServer(WebServiceGatewaySupport)createServer(ApplicationContext) を呼び出して、MockWebServiceServer インスタンスを作成します。
  2. expect(RequestMatcher) を呼び出して、おそらく RequestMatchers で提供されているデフォルトの RequestMatcher 実装(静的にインポート可能)を使用して、リクエストの期待値を設定します。ResponseActions.andExpect(RequestMatcher) 呼び出しを連鎖させることにより、複数の期待値を設定できます。
  3. andRespond(ResponseCreator) を呼び出して、おそらく ResponseCreators で提供されているデフォルトの ResponseCreator 実装(静的にインポート可能)を使用して、適切なレスポンスメッセージを作成します。
  4. WebServiceTemplate は、クライアントコードを介して直接、または通常どおりに使用します。
  5. verify() を呼び出します。
このクラス(および関連するクラス)によって提供される「流れるような」API のため、通常、IDE でコード補完機能(つまり、ctrl-space)を使用してモックを設定できることに注意してください。

例:

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
import org.springframework.xml.transform.StringSource;

import org.springframework.ws.test.client.MockWebServiceServer;
import static org.springframework.ws.test.client.RequestMatchers.*;
import static org.springframework.ws.test.client.ResponseCreators.*;
import static org.assertj.core.api.Assertions.assertThat;

@SpringJUnitConfig(locations = "applicationContext.xml")
public class MyWebServiceClientIntegrationTest {

 // MyWebServiceClient extends WebServiceGatewaySupport, and is configured in applicationContext.xml
 @Autowired
 private MyWebServiceClient client;

 private MockWebServiceServer mockServer;

 @Before
 public void createServer() throws Exception {
   mockServer = MockWebServiceServer.createServer(client);
 }

 @Test
 public void getCustomerCount() throws Exception {
   Source expectedRequestPayload =
         new StringSource("<customerCountRequest xmlns=\"http://springframework.org/spring-ws/test\" />");
   Source responsePayload = new StringSource("<customerCountResponse xmlns='http://springframework.org/spring-ws/test'>" +
         "<customerCount>10</customerCount>" +
         "</customerCountResponse>");

   mockServer.expect(payload(expectedRequestPayload)).andRespond(withPayload(responsePayload));

   // client.getCustomerCount() uses the WebServiceTemplate
   int customerCount = client.getCustomerCount();
   assertThat(response.getCustomerCount()).isEqualTo(10);

   mockServer.verify();
 }
}
導入:
2.0
  • コンストラクターの詳細

  • 方法の詳細

    • createServer

      public static MockWebServiceServer createServer(WebServiceTemplate webServiceTemplate)
      指定された WebServiceTemplate に基づいて MockWebServiceServer インスタンスを作成します。
      パラメーター:
      webServiceTemplate - Web サービステンプレート
      戻り値:
      作成されたサーバー
    • createServer

      public static MockWebServiceServer createServer(WebServiceGatewaySupport gatewaySupport)
      指定された WebServiceGatewaySupport に基づいて MockWebServiceServer インスタンスを作成します。
      パラメーター:
      gatewaySupport - クライアントクラス
      戻り値:
      作成されたサーバー
    • createServer

      public static MockWebServiceServer createServer(org.springframework.context.ApplicationContext applicationContext)
      指定された ApplicationContext に基づいて MockWebServiceServer インスタンスを作成します。

      このファクトリメソッドは、指定されたアプリケーションコンテキストで構成済みの WebServiceTemplate を見つけようとします。テンプレートが見つからない場合は、WebServiceGatewaySupport を見つけようとし、構成されたテンプレートを使用します。どちらも見つからない場合は、例外がスローされます。

      パラメーター:
      applicationContext - クライアントのベースとなるアプリケーションコンテキスト
      戻り値:
      作成されたサーバー
      例外:
      IllegalArgumentExceptionSE - 指定されたアプリケーションコンテキストに WebServiceTemplateWebServiceGatewaySupport も含まれていない場合。
    • expect

      public ResponseActions expect(RequestMatcher requestMatcher)
      指定された RequestMatcher によって指定された期待値を記録します。レスポンスの作成、またはより多くの期待値の設定を可能にする ResponseActions オブジェクトを返します。
      パラメーター:
      requestMatcher - リクエストマッチャーが期待されます
      戻り値:
      レスポンスアクション
    • verify

      public void verify()
      MockWebServiceMessageSender のすべての期待が満たされていることを確認します。
      例外:
      AssertionErrorSE - 期待が満たされない場合
    • reset

      public void reset()
      MockWebServiceMessageSender の期待をリセットします。