97. Migrations

Tip

有关最新的迁移指南,请访问项目的wiki page

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

97.1 1.0.x→1.1.x

本节介绍从 1.0 版升级到 1.1 版的过程。

97.1.1 生成的存根的新结构

1.1.x中,我们对生成的存根的结构进行了更改。如果您一直使用@AutoConfigureWireMock表示法来使用 Classpath 中的存根,那么它将不再起作用。以下示例显示@AutoConfigureWireMock表示法的工作原理:

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

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

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

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

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

Gradle.

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"
}

97.2 1.1.x→1.2.x

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

97.2.1 自定义 HttpServerStub

HttpServerStub包含不是 1.1 版中的方法。该方法是String registeredMappings()如果您具有实现HttpServerStub的类,则现在必须实现registeredMappings()方法。它应该返回一个String,表示一个HttpServerStub中所有可用的 Map。

有关更多详细信息,请参见issue 355

97.2.2 用于生成测试的新软件包

设置生成的测试程序包名称的流程如下所示:

  • 设置basePackageForTests

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

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

  • 如果未设置任何内容,请选择默认值:org.springframework.cloud.contract.verifier.tests

有关更多详细信息,请参见issue 260

97.2.3 TemplateProcessor 中的新方法

为了增加对fromRequest.path的支持,必须将以下方法添加到TemplateProcessor界面:

  • path()

  • path(int index)

有关更多详细信息,请参见issue 388

97.2.4 RestAssured 3.0

在生成的测试类中使用的“放心的 Rest”被撞到了3.0。如果您手动设置 Spring Cloud Contract 的版本和发行版,可能会看到以下异常:

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

由于使用旧版本的插件生成了测试,并且在测试执行时您拥有发行版的不兼容版本,因此会发生此异常。

通过issue 267完成

97.3 1.2.x→2.0.x