從位置獲取 Stub 或契約定義

除了從 Artifactory、Nexus 或 Git 中選擇 stub 或契約定義之外,您還可以指向驅動器上或類路徑上的某個位置。這在多模組專案中特別有用,其中一個模組希望重用另一個模組的 stub 或契約,而無需實際將這些內容安裝到本地 maven 倉庫或將這些更改提交到 Git。

為了實現這一點,當在 Stub Runner 或 Spring Cloud Contract 外掛中設定了倉庫根引數時,您可以使用 stubs:// 協議。

在此示例中,producer 專案已成功構建,並在 target/stubs 資料夾下生成了 stub。作為消費者,可以使用 stubs:// 協議設定 Stub Runner 從該位置獲取 stub。

註解
@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 可以儲存在一個位置,其中每個生產者都有自己專用的資料夾用於契約和 stub 對映。在該資料夾下,每個消費者都可以有自己的設定。為了讓 Stub Runner 根據提供的 ID 找到專用資料夾,您可以傳遞 stubs.find-producer=true 屬性或 stubrunner.stubs.find-producer=true 系統屬性。以下列表顯示了契約和 stub 的排列方式:

└── 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 artifact ID 為 [some-artifact-id] 的消費者
3 artifact ID 為 [some-artifact-id] 的消費者的契約
4 artifact ID 為 [some-artifact-id] 的消費者的對映
5 artifact 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 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());