|
该版本仍在开发中,尚未被视为稳定。对于最新的稳定版本,请使用 spring-cloud-contract 5.0.0! |
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
流动
流程看起来与《开发你的第一个春季云合约应用》中呈现的完全相同,但存根存储实现是一个 git 仓库。
你可以在文档的“作”页面了解更多关于搭建git仓库以及设置消费者端和生产端的内容。请参阅文档的“作”页面。
消费者设置
为了从 git 仓库获取存根,而不是从 Nexus 或 Artifactory 获取,你需要使用git协议中 URL 的repositoryRootStub Runner 中的属性。以下示例展示了如何设置:
- 注解
-
@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中实现:
- 梅文
-
<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仓库的内容。