在 Git 中使用 Stub 進行供應商合約測試

在這個流程中,我們執行供應商合約測試(生產者不知道消費者如何使用他們的 API)。Stub 上傳到單獨的儲存庫(它們不會上傳到 Artifactory 或 Nexus)。

先決條件

在 Git 中使用 Stub 測試供應商合約之前,您必須提供一個 Git 儲存庫,其中包含每個生產者的所有 Stub。有關此類專案的範例,請參閱此範例此範例。將 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 的消費者程式碼。有關此類專案的範例,請參閱此範例並搜尋 BeerControllerGitTest 測試。您還必須提供已設定 Spring Cloud Contract 以及外掛程式的生產者程式碼。有關此類專案的範例,請參閱此範例

流程

此流程與開發您的第一個基於 Spring Cloud Contract 的應用程式中呈現的流程完全相同,但 Stub Storage 實作是 Git 儲存庫。

您可以在文件的操作指南頁面中閱讀有關設定 Git 儲存庫以及設定消費者和生產者端的更多資訊。

消費者設定

為了從 Git 儲存庫而不是 Nexus 或 Artifactory 擷取 Stub,您需要在 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);

===

設定生產者

若要將 Stub 推送到 Git 儲存庫而不是 Nexus 或 Artifactory,您需要在外掛程式設定的 URL 中使用 git 協定。此外,您需要明確告知外掛程式在建置過程結束時推送 Stub。以下範例展示如何在 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 儲存庫的更多資訊。