在 Git 中使用存根进行提供者合同测试

在此流程中,我们执行提供者契约测试(生产者不知道使用者如何使用其 API)。存根将上传到单独的存储库(它们不会上传到 Artifactory 或 Nexus)。spring-doc.cadn.net.cn

前提条件

在测试 git 中带有存根的提供者合约之前,必须提供 git 存储库 其中包含每个生产者的所有存根。有关此类项目的示例,请参阅此示例此示例。 由于将存根推送到那里,存储库具有以下结构:spring-doc.cadn.net.cn

$ 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 的生产者代码 合同设置,以及插件。有关此类项目的示例,请参阅此示例spring-doc.cadn.net.cn

流动

该流程看起来与开发第一个基于 Spring Cloud Contract 的应用程序中介绍的流程完全相同, 但是Stub Storage实现是一个 Git 存储库。spring-doc.cadn.net.cn

您可以阅读有关设置 git 存储库和设置消费者和生产者端的更多信息 在文档的“作方法”页面中。spring-doc.cadn.net.cn

消费者设置

为了从 git 存储库而不是 Nexus 或 Artifactory 获取存根,您可以 需要使用git协议repositoryRootStub Runner 的房产。 以下示例演示如何设置它:spring-doc.cadn.net.cn

注解
@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,您需要 使用git协议。此外,您还需要明确告诉 在构建过程结束时推送存根的插件。以下示例显示 如何在 Maven 和 Gradle 中执行此作:spring-doc.cadn.net.cn

专家
<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 存储库的更多信息。spring-doc.cadn.net.cn