JAX-RS

Spring Cloud Contract は、JAX-RS 2 クライアント API をサポートします。基本クラスでは、protected WebTarget webTarget とサーバーの初期化を定義する必要があります。JAX-RS API をテストする唯一のオプションは、Web サーバーを起動することです。また、ボディのあるリクエストにはコンテンツ型を設定する必要があります。それ以外の場合は、デフォルトの application/octet-stream が使用されます。

JAX-RS モードを使用するには、次の設定を使用します。

testMode = 'JAXRSCLIENT'

次の例は、生成されたテスト API を示しています。

import com.jayway.jsonpath.DocumentContext;
import com.jayway.jsonpath.JsonPath;
import org.junit.Test;
import org.junit.Rule;
import javax.ws.rs.client.Entity;
import javax.ws.rs.core.Response;

import static org.springframework.cloud.contract.verifier.assertion.SpringCloudContractAssertions.assertThat;
import static org.springframework.cloud.contract.verifier.util.ContractVerifierUtil.*;
import static com.toomuchcoding.jsonassert.JsonAssertion.assertThatJson;
import static javax.ws.rs.client.Entity.*;

public class FooTest {
  WebTarget webTarget;

  @Test
  public void validate_() throws Exception {

    // when:
      Response response = webTarget
              .path("/users")
              .queryParam("limit", "10")
              .queryParam("offset", "20")
              .queryParam("filter", "email")
              .queryParam("sort", "name")
              .queryParam("search", "55")
              .queryParam("age", "99")
              .queryParam("name", "Denis.Stepanov")
              .queryParam("email", "[email protected] (英語)  ")
              .request()
              .build("GET")
              .invoke();
      String responseAsString = response.readEntity(String.class);

    // then:
      assertThat(response.getStatus()).isEqualTo(200);

    // and:
      DocumentContext parsedJson = JsonPath.parse(responseAsString);
      assertThatJson(parsedJson).field("['property1']").isEqualTo("a");
  }

}