공식 레퍼런스를 한글로 번역한 문서입니다.
전체 목차는 여기에 있습니다.
이번에는 서비스 제공자provider 주도로 명세contract 테스트를 진행해본다 (프로듀서producer는 컨슈머consumer가 API를 어떻게 사용하는지는 알 수 없다). 스텁stub은 별도 레포지토리에 업로드한다 (Artifactory나 Nexus에는 업로드하지 않는다).
목차
2.2.1. Prerequisites
git에 있는 스텁stub을 사용해 서비스 제공자provider 명세contract를 테스트하려면 먼저, 각 프로듀서producer의 모든 스텁stub이 포함된 git 레포지토리를 제공해야 한다. 샘플 프로젝트가 필요하다면, 이곳을 참고해라. 레포지토리에 스텁stub을 푸시할 때는 다음과 같은 구조로 푸시한다:
$ tree .
└── META-INF
└── folder.with.group.id.as.its.name
└── folder-with-artifact-id
└── folder-with-version
├── contractA.groovy
├── contractB.yml
└── contractC.groovy
그 외에 Spring Cloud Contract Stub Runner를 설정해둔 컨슈머consumer 코드도 제공해야 한다. 샘플 코드는 이곳에 있는 BeerControllerGitTest
를 참고해라. 마찬가지로, Spring Cloud Contract와 플러그인을 설정한 프로듀서producer 코드도 함께 필요하다. 샘플 프로젝트는 여기를 참고해라.
2.2.2. The Flow
여기에서 진행해볼 작업들은, git 레포지토리를 Stub Storage
로 사용한다는 점만 빼면 처음 만드는 Spring Cloud Contract 애플리케이션에서 설명한 것과 완전히 동일하다.
git 레포지토리 세팅 방법이나 컨슈머consumer, 프로듀서producer 설정에 대한 자세한 내용은 이 문서에 있는 How To 섹션에서 확인할 수 있다.
2.2.3. Consumer setup
Nexus나 Artifactory가 아닌 git 레포지토리에서 스텁stub을 가져오려면, Stub Runner의 repositoryRoot
프로퍼티에 git
프로토콜을 사용하는 URL을 설정해줘야 한다. 설정 방법은 다음 예제를 참고해라:
@AutoConfigureStubRunner(
stubsMode = StubRunnerProperties.StubsMode.REMOTE,
repositoryRoot = "git://git@github.com:spring-cloud-samples/spring-cloud-contract-nodejs-contracts-git.git",
ids = "com.example:artifact-id:0.0.1")
@Rule
public StubRunnerRule rule = new StubRunnerRule()
.downloadStub("com.example","artifact-id", "0.0.1")
.repoRoot("git://git@github.com:spring-cloud-samples/spring-cloud-contract-nodejs-contracts-git.git")
.stubsMode(StubRunnerProperties.StubsMode.REMOTE);
@RegisterExtension
public StubRunnerExtension stubRunnerExtension = new StubRunnerExtension()
.downloadStub("com.example","artifact-id", "0.0.1")
.repoRoot("git://git@github.com:spring-cloud-samples/spring-cloud-contract-nodejs-contracts-git.git")
.stubsMode(StubRunnerProperties.StubsMode.REMOTE);
2.2.4. Setting up the Producer
스텁stub을 Nexus나 Artifactory 대신 git 레포지토리로 푸시하려면, 플러그인 설정에 git 프로토콜 URL을 지정해야 한다. 또한, 빌드 프로세스가 끝날 때 스텁stub을 푸시하도록 플러그인에 명시해줘야 한다. 다음은 각각 Maven과 Gradle을 사용하는 예시다:
<plugin>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-contract-maven-plugin</artifactId>
<version>${spring-cloud-contract.version}</version>
<extensions>true</extensions>
<configuration>
<!-- Base class mappings etc. -->
<!-- We want to pick contracts from a Git repository -->
<contractsRepositoryUrl>git://git://git@github.com:spring-cloud-samples/spring-cloud-contract-nodejs-contracts-git.git</contractsRepositoryUrl>
<!-- We reuse the contract dependency section to set up the path
to the folder that contains the contract definitions. In our case the
path will be /groupId/artifactId/version/contracts -->
<contractDependency>
<groupId>${project.groupId}</groupId>
<artifactId>${project.artifactId}</artifactId>
<version>${project.version}</version>
</contractDependency>
<!-- The contracts mode can't be classpath -->
<contractsMode>REMOTE</contractsMode>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<!-- By default we will not push the stubs back to SCM,
you have to explicitly add it as a goal -->
<goal>pushStubsToScm</goal>
</goals>
</execution>
</executions>
</plugin>
contracts {
// We want to pick contracts from a Git repository
contractDependency {
stringNotation = "${project.group}:${project.name}:${project.version}"
}
/*
We reuse the contract dependency section to set up the path
to the folder that contains the contract definitions. In our case the
path will be /groupId/artifactId/version/contracts
*/
contractRepository {
repositoryUrl = "git://git://git@github.com:spring-cloud-samples/spring-cloud-contract-nodejs-contracts-git.git"
}
// The mode can't be classpath
contractsMode = "REMOTE"
// Base class mappings etc.
}
/*
In this scenario we want to publish stubs to SCM whenever
the `publish` task is run
*/
publish.dependsOn("publishStubsToScm")
git 레포지토리 설정에 대한 자세한 내용은 이 문서에 있는 How To 섹션에서 확인할 수 있다.
Next :
2.3. Consumer Driven Contracts with Contracts on the Producer Side
프로듀서 레포지토리에 있는 명세로 CDC 실행해보기
전체 목차는 여기에 있습니다.