場所からのスタブまたは契約定義の取得

Artifactory、Nexus、Git からスタブまたは契約定義を選択する代わりに、ドライブまたはクラスパス上の場所を指定できます。これは、変更を Git にコミットするためにローカルの maven リポジトリに実際にインストールする必要がなく、あるモジュールが別のモジュールのスタブまたは契約を再利用したい場合に特に便利です。

これを実現するには、リポジトリルートパラメーターがスタブランナーまたは Spring Cloud Contract プラグインで設定されているときに stubs:// プロトコルを使用できます。

この例では、producer プロジェクトが正常にビルドされ、target/stubs フォルダーにスタブが生成されています。コンシューマーは、stubs:// プロトコルを使用してその場所からスタブを選択するようにスタブランナーをセットアップできます。

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);

契約とスタブは、各プロデューサーが契約とスタブのマッピング用の独自の専用フォルダーを持つ場所に保存できます。そのフォルダーで、各コンシューマーは独自のセットアップを行うことができます。スタブランナーに指定された 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());