從位置擷取 Stub 或合約定義

您可以指向磁碟機或類別路徑上的位置,而不是從 Artifactory、Nexus 或 Git 中選取 Stub 或合約定義。這樣做在多模組專案中特別有用,在這種專案中,一個模組想要重複使用另一個模組的 Stub 或合約,而無需實際將這些模組安裝在本機 Maven 儲存庫中,以將這些變更提交到 Git。

為了達到此目的,當在 Stub Runner 或 Spring Cloud Contract 外掛程式中設定儲存庫根參數時,您可以使用 stubs:// 通訊協定。

在此範例中,producer 專案已成功建置,並且 Stub 已在 target/stubs 資料夾下產生。作為消費者,可以設定 Stub Runner 以使用 stubs:// 通訊協定從該位置選取 Stub。

註解
@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 可以儲存在一個位置,其中每個生產者都有自己的專用資料夾用於合約和 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 消費者的群組 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 規則
	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());