91. 迁移

本节介绍从 Spring Cloud Contract Verifier 的一个 version 迁移到下一个 version。它涵盖了以下版本的升级 paths:

91.1 1.0.x→1.1.x

本节介绍从 version 1.0 升级到 version 1.1.

91.1.1 生成的存根的新结构

1.1.x中,我们引入了对生成的存根的结构的更改。如果您一直使用@AutoConfigureWireMock表示法来使用 classpath 中的存根,则它不再有效。以下 example 显示了@AutoConfigureWireMock符号如何工作:

@AutoConfigureWireMock(stubs = "classpath:/customer-stubs/mappings", port = 8084)

您必须将存根的位置更改为:classpath:…/META-INF/groupId/artifactId/version/mappings或使用新的 classpath-based @AutoConfigureStubRunner,如以下 example 所示:

@AutoConfigureWireMock(stubs = "classpath:customer-stubs/META-INF/travel.components/customer-contract/1.0.2-SNAPSHOT/mappings/", port = 8084)

如果您不想使用@AutoConfigureStubRunner并且希望保留旧结构,请相应地设置插件任务。以下 example 适用于上一个代码段中显示的结构。

Maven 的.

<!-- start of pom.xml -->

<properties>
    <!-- we don't want the verifier to do a jar for us -->
    <spring.cloud.contract.verifier.skip>true</spring.cloud.contract.verifier.skip>
</properties>

<!-- ... -->

<!-- You need to set up the assembly plugin -->
<build>
    <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>
                        <descriptor>$../../../../src/assembly/stub.xml</descriptor>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>
<!-- end of pom.xml -->

<!-- start of 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}/snippets/stubs</directory>
			<outputDirectory>customer-stubs/mappings</outputDirectory>
			<includes>
				<include>**/*</include>
			</includes>
		</fileSet>
		<fileSet>
			<directory>$../../../../src/test/resources/contracts</directory>
			<outputDirectory>customer-stubs/contracts</outputDirectory>
			<includes>
				<include>**/*.groovy</include>
			</includes>
		</fileSet>
	</fileSets>
</assembly>

<!-- end of stub.xml-->

摇篮.

task copyStubs(type: Copy, dependsOn: 'generateWireMockClientStubs') {
//    Preserve directory structure from 1.0.X of spring-cloud-contract
    from "${project.buildDir}/resources/main/customer-stubs/META-INF/${project.group}/${project.name}/${project.version}"
    into "${project.buildDir}/resources/main/customer-stubs"
}

91.2 1.1.x→1.2.x

本节介绍从 version 1.1 升级到 version 1.2.

91.2.1 自定义 HttpServerStub

HttpServerStub包含一个不在 version 1.1 中的方法。方法是String registeredMappings()如果你有 class 实现HttpServerStub,你现在必须实现registeredMappings()方法。它应该_ret 表示单个HttpServerStub中可用的所有映射。

有关详细信息,请参阅问题 355

91.2.2 生成测试的新包

设置生成的测试包 name 的流程如下所示:

  • 设置basePackageForTests

  • 如果未设置basePackageForTests,请从baseClassForTests中选择包

  • 如果未设置baseClassForTests,请选择packageWithBaseClasses

  • 如果没有设置,请选择默认的 value:org.springframework.cloud.contract.verifier.tests

有关详细信息,请参阅问题 260

91.2.3 TemplateProcessor 中的新方法

在 order 中添加对fromRequest.path的支持,必须将以下方法添加到TemplateProcessor接口:

  • path()

  • path(int index)

有关详细信息,请参阅问题 388

91.2.4 RestAssured 3.0

Rest Assured,在生成的测试 classes 中使用,被撞到了3.0。如果您手动设置 Spring Cloud Contract 和版本系列的版本,您可能会看到以下 exception:

Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:testCompile (default-testCompile) on project some-project: Compilation failure: Compilation failure:
[ERROR] /some/path/SomeClass.java:[4,39] package com.jayway.restassured.response does not exist

这个 exception 将会发生,因为测试是使用旧的 version 插件生成的,并且在测试执行时 time 你有一个与版本系列不兼容的 version(反之亦然)。

通过问题 267完成