|
此版本仍在开发中,目前尚不被视为稳定版本。如需最新稳定版本,请使用 spring-cloud-contract 5.0.2! |
从位置获取存根或契约定义
与其从 Artifactory、Nexus 或 Git 中选择存根(stubs)或契约(contract)定义,您也可以指向驱动器上的某个位置或类路径(classpath)。在多模块项目中,这种做法尤其有用:一个模块可重用另一个模块中的存根或契约,而无需将这些存根或契约实际安装到本地 Maven 仓库中,也无需将其变更提交到 Git。
为了实现这一点,当存储库根参数在 Stub Runner 或 Spring Cloud Contract 插件中设置时,您可以使用 stubs:// 协议。
在本示例中,producer 项目已成功构建,并在 target/stubs 文件夹下生成了存根(stubs)。作为消费者,可通过使用 stubs:// 协议,将 Stub Runner 配置为从该位置加载存根。
注解
@AutoConfigureStubRunner(
stubsMode = StubRunnerProperties.StubsMode.REMOTE,
repositoryRoot = "stubs://file://location/to/the/producer/target/stubs/",
ids = "com.example:some-producer")
JUnit 4 规则
@Rule
public StubRunnerRule rule = new StubRunnerRule()
.downloadStub("com.example:some-producer")
.repoRoot("stubs://file://location/to/the/producer/target/stubs/")
.stubsMode(StubRunnerProperties.StubsMode.REMOTE);
JUnit 5 扩展
@RegisterExtension
public StubRunnerExtension stubRunnerExtension = new StubRunnerExtension()
.downloadStub("com.example:some-producer")
.repoRoot("stubs://file://location/to/the/producer/target/stubs/")
.stubsMode(StubRunnerProperties.StubsMode.REMOTE);
契约和存根可以存储在一个位置,其中每个生产者都有其专用的文件夹来存放契约和存根映射。在该文件夹下,每个消费者都可以拥有自己的设置。为了使 Stub Runner 能够根据提供的 ID 找到专用文件夹,您可以传递 stubs.find-producer=true 属性或 spring.cloud.contract.stubrunner.stubs.find-producer=true 系统属性。
以下列表展示了一种契约和存根的组织结构:
└── com.example (1)
├── some-artifact-id (2)
│ └── 0.0.1
│ ├── contracts (3)
│ │ └── shouldReturnStuffForArtifactId.groovy
│ └── mappings (4)
│ └── shouldReturnStuffForArtifactId.json
└── some-other-artifact-id (5)
├── contracts
│ └── shouldReturnStuffForOtherArtifactId.groovy
└── mappings
└── shouldReturnStuffForOtherArtifactId.json
| 1 | 消费者的组 ID |
| 2 | 消费者,构件ID为 [some-artifact-id] |
| 3 | 消费者与构件 ID [some-artifact-id] 的合同 |
| 4 | 消费者构件 ID [some-artifact-id] 的映射 |
| 5 | 消费者,构件ID为 [some-other-artifact-id] |
注解
@AutoConfigureStubRunner(
stubsMode = StubRunnerProperties.StubsMode.REMOTE,
repositoryRoot = "stubs://file://location/to/the/contracts/directory",
ids = "com.example:some-producer",
properties="stubs.find-producer=true")
JUnit 4 规则
static Map<String, String> contractProperties() {
Map<String, String> map = new HashMap<>();
map.put("stubs.find-producer", "true");
return map;
}
@Rule
public StubRunnerRule rule = new StubRunnerRule()
.downloadStub("com.example:some-producer")
.repoRoot("stubs://file://location/to/the/contracts/directory")
.stubsMode(StubRunnerProperties.StubsMode.REMOTE)
.properties(contractProperties());
JUnit 5 扩展
static Map<String, String> contractProperties() {
Map<String, String> map = new HashMap<>();
map.put("stubs.find-producer", "true");
return map;
}
@RegisterExtension
public StubRunnerExtension stubRunnerExtension = new StubRunnerExtension()
.downloadStub("com.example:some-producer")
.repoRoot("stubs://file://location/to/the/contracts/directory")
.stubsMode(StubRunnerProperties.StubsMode.REMOTE)
.properties(contractProperties());