Fetching Stubs or Contract Definitions From A Location
您无需从 Artifactory、Nexus 或 Git 中选择存根或合约定义,而是可以指向驱动器或类路径上的位置。这样做在多模块项目中特别有用,其中一个模块需要重用另一个模块的存根或合约,而无需实际将它们安装到本地 maven 存储库以将这些更改提交到 Git。
为了实现此目的,当在 Stub Runner 或 Spring Cloud Contract 插件中设置存储库根参数时,可以使用“stubs://”协议。
在此示例中,“producer”项目已成功构建,并且在“target/stubs”文件夹下生成了存根。作为使用者,可以通过使用“stubs://”协议设置 Stub Runner 从该位置选取存根。
Annotation
@AutoConfigureStubRunner(
stubsMode = StubRunnerProperties.StubsMode.REMOTE,
repositoryRoot = "stubs://file://location/to/the/producer/target/stubs/",
ids = "com.example:some-producer")
JUnit 4 Rule
@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 Extension
@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”属性或“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] 的消费者 |
Annotation
@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 Rule
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 Extension
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());