该版本仍在开发中,尚未被视为稳定。对于最新的稳定版本,请使用 spring-cloud-contract 5.0.0!spring-doc.cadn.net.cn

Git中的提供商合同测试,带有存根

在这个流程中,我们会进行提供者合同测试(生产者对消费者如何使用他们的 API 一无所知)。这些存根会上传到单独的仓库(它们不会上传到 Artifactory 或 Nexus)。spring-doc.cadn.net.cn

前提条件

在测试带有 stub 的提供商合同之前,你必须提供一个 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合同存根的消费者代码。为 此类项目的示例,请参见此示例并搜索BeerControllerGitTest测试。你还必须提供带有Spring Cloud的生产者代码 合同设置,加上一个插件。关于此类项目的示例,请参见此示例spring-doc.cadn.net.cn

流动

流程看起来与《开发你的第一个春季云合约应用》中呈现的完全相同, 但存根存储实现是一个 git 仓库。spring-doc.cadn.net.cn

你可以了解更多关于如何搭建git仓库以及设置消费者端和生产端的内容 在文档的“作指南”页面spring-doc.cadn.net.cn

消费者设置

如果你想从git仓库里获取stub,而不是Nexus或Artifactory,你 需要使用git协议中 URL 的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插件设置的URL中包含协议。另外你需要明确说明 插件是在构建过程结束时推送存根的。以下示例显示 如何在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>
格拉德勒
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