Git のスタブを使用したプロバイダー契約のテスト

このフローでは、プロバイダー契約のテストを実行します (プロデューサーはコンシューマーが API をどのように使用するかについて知りません)。スタブは別のリポジトリにアップロードされます (Artifactory や Nexus にはアップロードされません)。

前提条件

git でスタブを使用してプロバイダー 契約をテストする前に、各プロデューサーのすべてのスタブを含む git リポジトリを提供する必要があります。このようなプロジェクトの例については、このサンプル [GitHub] (英語) またはこのサンプル [GitHub] (英語) を参照してください。スタブをそこにプッシュした結果、リポジトリは次の構造になります。

$ tree .
└── META-INF
   └── folder.with.group.id.as.its.name
       └── folder-with-artifact-id
           └── folder-with-version
               ├── contractA.groovy
               ├── contractB.yml
               └── contractC.groovy

Spring Cloud Contract Stub Runner がセットアップされたコンシューマーコードも提供する必要があります。このようなプロジェクトの例については、このサンプル [GitHub] (英語) を参照し、BeerControllerGitTest テストを検索してください。Spring Cloud Contract がセットアップされたプロデューサーコードとプラグインも提供する必要があります。このようなプロジェクトの例については、このサンプル [GitHub] (英語) を参照してください。

流れ

フローは初めての Spring Cloud Contract ベースのアプリケーションの開発で示されているものとまったく同じですが、Stub Storage 実装は git リポジトリです。

git リポジトリのセットアップとコンシューマー側とプロデューサー側の設定の詳細については、ドキュメントの使い方ページを参照してください。

コンシューマー向けセットアップ

Nexus や Artifactory ではなく git リポジトリからスタブを取得するには、Stub Runner の repositoryRoot プロパティの URL で git プロトコルを使用する必要があります。次の例は、その設定方法を示しています。

=== アノテーション ::

+

@AutoConfigureStubRunner(
stubsMode = StubRunnerProperties.StubsMode.REMOTE,
		repositoryRoot = "git://[email protected] (英語)  :spring-cloud-samples/spring-cloud-contract-nodejs-contracts-git.git",
		ids = "com.example:artifact-id:0.0.1")
JUnit 4 ルール
@Rule
	public StubRunnerRule rule = new StubRunnerRule()
			.downloadStub("com.example","artifact-id", "0.0.1")
			.repoRoot("git://[email protected] (英語)  :spring-cloud-samples/spring-cloud-contract-nodejs-contracts-git.git")
			.stubsMode(StubRunnerProperties.StubsMode.REMOTE);
JUnit 5 拡張機能
@RegisterExtension
	public StubRunnerExtension stubRunnerExtension = new StubRunnerExtension()
			.downloadStub("com.example","artifact-id", "0.0.1")
			.repoRoot("git://[email protected] (英語)  :spring-cloud-samples/spring-cloud-contract-nodejs-contracts-git.git")
			.stubsMode(StubRunnerProperties.StubsMode.REMOTE);

===

プロデューサーのセットアップ

スタブを Nexus や Artifactory ではなく git リポジトリにプッシュするには、プラグインセットアップの URL で git プロトコルを使用する必要があります。また、ビルドプロセスの最後にスタブをプッシュするようにプラグインに明示的に指示する必要があります。次の例は、MavenGradle の両方でこれを行う方法を示しています。

=== Maven::

+

<plugin>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-contract-maven-plugin</artifactId>
    <version>${spring-cloud-contract.version}</version>
    <extensions>true</extensions>
    <configuration>
        <!-- Base class mappings etc. -->

        <!-- We want to pick contracts from a Git repository -->
        <contractsRepositoryUrl>git://git://[email protected] (英語)  :spring-cloud-samples/spring-cloud-contract-nodejs-contracts-git.git</contractsRepositoryUrl>

        <!-- We reuse the contract dependency section to set up the path
        to the folder that contains the contract definitions. In our case the
        path will be /groupId/artifactId/version/contracts -->
        <contractDependency>
            <groupId>${project.groupId}</groupId>
            <artifactId>${project.artifactId}</artifactId>
            <version>${project.version}</version>
        </contractDependency>

        <!-- The contracts mode can't be classpath -->
        <contractsMode>REMOTE</contractsMode>
    </configuration>
    <executions>
        <execution>
            <phase>package</phase>
            <goals>
                <!-- By default we will not push the stubs back to SCM,
                you have to explicitly add it as a goal -->
                <goal>pushStubsToScm</goal>
            </goals>
        </execution>
    </executions>
</plugin>
Gradle
contracts {
	// We want to pick contracts from a Git repository
	contractDependency {
		stringNotation = "${project.group}:${project.name}:${project.version}"
	}
	/*
	We reuse the contract dependency section to set up the path
	to the folder that contains the contract definitions. In our case the
	path will be /groupId/artifactId/version/contracts
	 */
	contractRepository {
		repositoryUrl = "git://git://[email protected] (英語)  :spring-cloud-samples/spring-cloud-contract-nodejs-contracts-git.git"
	}
	// The mode can't be classpath
	contractsMode = "REMOTE"
	// Base class mappings etc.
}

/*
In this scenario we want to publish stubs to SCM whenever
the `publish` task is run
*/
publish.dependsOn("publishStubsToScm")

===

git リポジトリのセットアップの詳細については、ドキュメントの使い方セクションを参照してください。