从某个位置获取存根或合同定义
而不是从 您可以指向 Artifactory、Nexus 或 Git 驱动器或 Classpath 上的位置。这样做在多模块项目中特别有用,因为一个模块需要 重用来自另一个模块的存根或 Contract 而不 需要在本地 maven 中实际安装这些 存储库将这些更改提交到 Git。
为了实现此目的,您可以在设置存储库根参数时使用协议
在 Stub Runner 或 Spring Cloud Contract 插件中。stubs://
在此示例中,项目已成功
built 并在该文件夹下生成存根。作为使用者,可以使用协议设置 Stub Runner 以从该位置选取存根。producer
target/stubs
stubs://
注解
@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);
Contract 和 stub 可以存储在一个位置,每个 producer 都有自己的专用文件夹,用于 Contract 和 stub 映射。在该文件夹下,每个使用者都可以有自己的设置。要使 Stub Runner 从提供的 ID 中查找专用文件夹,您可以传递 property 或 system 属性。
下面的清单显示了 Contract 和 stub 的排列:stubs.find-producer=true
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 | 消费者的 Group 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());