使用 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 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 擴充套件
-
@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 儲存庫的更多資訊。