在 Nexus 或 Artifactory 中使用 REST 文档和存根进行提供商合同测试

在此流程中,我们不使用 Spring Cloud Contract 插件来生成测试和存根。我们编写 Spring RESTDocs,并从中自动生成存根。最后,我们设置构建来打包存根并将它们上传到存根存储站点 — 在我们的例子中,是 Nexus 或 Artifactory。spring-doc.cn

生产者流

作为生产商,我们:spring-doc.cn

  1. 编写 API 的 RESTDocs 测试。spring-doc.cn

  2. 将 Spring Cloud Contract Stub Runner starter 添加到我们的构建 () 中,如下所示:spring-cloud-starter-contract-stub-runnerspring-doc.cn

    Maven 系列
    <dependencies>
    	<dependency>
    		<groupId>org.springframework.cloud</groupId>
    		<artifactId>spring-cloud-starter-contract-stub-runner</artifactId>
    		<scope>test</scope>
    	</dependency>
    </dependencies>
    
    <dependencyManagement>
    	<dependencies>
    		<dependency>
    			<groupId>org.springframework.cloud</groupId>
    			<artifactId>spring-cloud-dependencies</artifactId>
    			<version>${spring-cloud.version}</version>
    			<type>pom</type>
    			<scope>import</scope>
    		</dependency>
    	</dependencies>
    </dependencyManagement>
    Gradle
    dependencies {
    	testImplementation 'org.springframework.cloud:spring-cloud-starter-contract-stub-runner'
    }
    
    dependencyManagement {
    	imports {
    		mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
    	}
    }
  3. 我们设置构建工具来打包我们的存根,如下所示:spring-doc.cn

    Maven 系列
    <!-- pom.xml -->
    <plugins>
    	<plugin>
    		<groupId>org.apache.maven.plugins</groupId>
    		<artifactId>maven-assembly-plugin</artifactId>
    		<executions>
    			<execution>
    				<id>stub</id>
    				<phase>prepare-package</phase>
    				<goals>
    					<goal>single</goal>
    				</goals>
    				<inherited>false</inherited>
    				<configuration>
    					<attach>true</attach>
    					<descriptors>
    						${basedir}/src/assembly/stub.xml
    					</descriptors>
    				</configuration>
    			</execution>
    		</executions>
    	</plugin>
    </plugins>
    
    <!-- src/assembly/stub.xml -->
    <assembly
    	xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3"
    	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    	xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3 http://maven.apache.org/xsd/assembly-1.1.3.xsd">
    	<id>stubs</id>
    	<formats>
    		<format>jar</format>
    	</formats>
    	<includeBaseDirectory>false</includeBaseDirectory>
    	<fileSets>
    		<fileSet>
    			<directory>${project.build.directory}/generated-snippets/stubs</directory>
    			<outputDirectory>META-INF/${project.groupId}/${project.artifactId}/${project.version}/mappings</outputDirectory>
    			<includes>
    				<include>**/*</include>
    			</includes>
    		</fileSet>
    	</fileSets>
    </assembly>
    Gradle
    task stubsJar(type: Jar) {
    	classifier = "stubs"
    	into("META-INF/${project.group}/${project.name}/${project.version}/mappings") {
    		include('**/*.*')
    		from("${project.buildDir}/generated-snippets/stubs")
    	}
    }
    // we need the tests to pass to build the stub jar
    stubsJar.dependsOn(test)
    bootJar.dependsOn(stubsJar)

现在,当我们运行测试时,存根会自动发布和打包。spring-doc.cn

以下 UML 图显示了生产者流程:spring-doc.cn

流提供程序 rest-docs-producer

消费者流程

由于使用者流不受用于生成存根的工具的影响,因此您可以阅读开发您的第一个基于 Spring Cloud Contract 的应用程序,以查看在 Nexus 或 Artifactory 中使用存根进行提供者 Contract 测试的使用者端的流程。spring-doc.cn