使用 Git 中的存根進行提供者契約測試
在此流程中,我們執行提供者契約測試(生產者不知道消費者如何使用其 API)。存根被上傳到單獨的倉庫(而不是上傳到 Artifactory 或 Nexus)。
先決條件
$ 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 Storage
實現是一個 git 倉庫。
您可以在文件的操作指南頁面中瞭解更多關於設定 git 倉庫以及設定消費者和生產者端的資訊。
消費者端設定
為了從 git 倉庫而不是 Nexus 或 Artifactory 獲取存根,您需要在 Stub Runner 的 repositoryRoot
屬性的 URL 中使用 git
協議。以下示例展示瞭如何進行設定:
- 註解
-
@AutoConfigureStubRunner( stubsMode = StubRunnerProperties.StubsMode.REMOTE, repositoryRoot = "git://[email protected]:spring-cloud-samples/spring-cloud-contract-nodejs-contracts-git.git", ids = "com.example:artifact-id:0.0.1")
- JUnit 4 Rule
-
@Rule public StubRunnerRule rule = new StubRunnerRule() .downloadStub("com.example","artifact-id", "0.0.1") .repoRoot("git://[email protected]:spring-cloud-samples/spring-cloud-contract-nodejs-contracts-git.git") .stubsMode(StubRunnerProperties.StubsMode.REMOTE);
- JUnit 5 Extension
-
@RegisterExtension public StubRunnerExtension stubRunnerExtension = new StubRunnerExtension() .downloadStub("com.example","artifact-id", "0.0.1") .repoRoot("git://[email protected]:spring-cloud-samples/spring-cloud-contract-nodejs-contracts-git.git") .stubsMode(StubRunnerProperties.StubsMode.REMOTE);
生產者端設定
要將存根推送到 git 倉庫而不是 Nexus 或 Artifactory,您需要在外掛設定的 URL 中使用 git
協議。此外,您還需要明確告訴外掛在構建過程結束時推送存根。以下示例展示瞭如何在 Maven 和 Gradle 中完成此操作:
- Maven
-
<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://[email protected]: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>
- Gradle
-
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://[email protected]: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 倉庫的資訊。