스프링 클라우드 컨트랙트 공식 레퍼런스를 한글로 번역한 문서입니다.
전체 목차는 여기에 있습니다.
Spring Cloud Contract는 JAX-RS 2 클라이언트 API를 지원한다. 테스트용 베이스 클래스에선 protected WebTarget webTarget
을 정의해야 하며, 서버를 초기화해야 한다. JAX-RS API를 테스트하려면 웹 서버를 시작하는 방법밖에 없다. 또한 body가 있는 요청이라면, content type을 설정해야 한다. 그렇지 않으면 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", "bob@email.com")
.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");
}
}
Next :
3.4.2. WebFlux with WebTestClient
WebTestClient를 이용해 WebFlux 애플리케이션에 Spring Cloud Contract 적용하기
전체 목차는 여기에 있습니다.