此版本仍在开发中,尚未被视为稳定版本。对于最新的稳定版本,请使用 spring-cloud-contract 4.1.5spring-doc.cadn.net.cn

Spring Cloud Contract WireMock

Spring Cloud Contract WireMock 模块允许您在 Spring Boot 应用程序。有关更多详细信息,请查看 Spring Cloud Contract 的 repository samples 子文件夹spring-doc.cadn.net.cn

如果您有一个使用 Tomcat 作为嵌入式服务器的 Spring Boot 应用程序(即 默认值为spring-boot-starter-web),您可以添加spring-cloud-starter-contract-stub-runner添加到您的 Classpath 中,并添加@AutoConfigureWireMock在测试中使用 Wiremock。Wiremock 作为存根服务器运行,而您 可以通过使用 Java API 或使用静态 JSON 声明作为 你的测试。spring-doc.cadn.net.cn

要在不同的端口上启动存根服务器,请使用(例如)、@AutoConfigureWireMock(port=9999).对于随机端口,请使用0.存根 服务器端口可以在测试应用程序上下文中使用wiremock.server.port财产。用@AutoConfigureWireMock添加WiremockConfiguration自 测试应用程序上下文,缓存在方法和类之间 具有相同的上下文。Spring 集成测试也是如此。此外,您还可以 注入一个WireMockServer添加到您的测试中。 在每个测试类之后,将重置已注册的 WireMock 服务器。 但是,如果您需要在每个测试方法之后重置它,请将wiremock.reset-mappings-after-each-testproperty 设置为true.spring-doc.cadn.net.cn

自动注册 Stub

如果您使用@AutoConfigureWireMock,它会从文件中注册 WireMock JSON 存根 system 或 classpath 中(默认情况下,从file:src/test/resources/mappings).您可以 使用stubs属性,可以是 Ant 样式的资源模式或目录。对于目录,*/.json是 附加。下面的代码显示了一个示例:spring-doc.cadn.net.cn

@RunWith(SpringRunner.class)
@SpringBootTest
@AutoConfigureWireMock(stubs="classpath:/stubs")
public class WiremockImportApplicationTests {

	@Autowired
	private Service service;

	@Test
	public void contextLoads() throws Exception {
		assertThat(this.service.go()).isEqualTo("Hello World!");
	}

}
实际上,WireMock 总是从src/test/resources/mappings 如 以及stubs属性。要更改此行为,您可以 此外,指定 File Root,如本文档的下一节所述。
此外,Git.stubslocation 不被视为 Wiremock 的“默认映射”的一部分,并且调用 自com.github.tomakehurst.wiremock.client.WireMock.resetToDefaultMappings在测试期间,不会导致映射 在stubs位置。但是,org.springframework.cloud.contract.wiremock.WireMockTestExecutionListener在每个测试类之后重置映射(包括从 stubs 位置添加映射),并且(可选)重置 在每个测试方法(由wiremock.reset-mappings-after-each-test属性)。

如果您使用 Spring Cloud Contract 的默认存根 jar,则您的 存根存储在/META-INF/group-id/artifact-id/versions/mappings/文件夹。 如果要从该位置注册所有嵌入式 JAR 中的所有存根,可以使用 以下语法:spring-doc.cadn.net.cn

@AutoConfigureWireMock(port = 0, stubs = "classpath*:/META-INF...

使用文件指定存根主体

WireMock 可以从 Classpath 或文件系统上的文件中读取响应体。在 的情况下,您可以在 JSON DSL 中看到响应具有bodyFileName而不是 (文字)body.这些文件是相对于根目录解析的(默认情况下,src/test/resources/__files).要自定义此位置,您可以设置files属性中的@AutoConfigureWireMockannotation 添加到父级的位置 目录(换句话说,__files是一个子目录)。您可以使用 Spring 资源 要引用的符号file:…​classpath:…​地点。通用 URL 不是 支持。可以给出值列表 — 在这种情况下,WireMock 解析第一个文件 当它需要查找响应正文时,它就存在了。spring-doc.cadn.net.cn

当您配置filesroot 的 Git 中,它还会影响 自动加载存根,因为它们来自根位置 在名为mappings.
的值files没有 对从stubs属性。

替代方案:使用 JUnit 规则

要获得更传统的 WireMock 体验,您可以使用 JUnit@Rules启动和停止 服务器。为此,请使用WireMockSpring便利类获取Optionsinstance,如下所示:spring-doc.cadn.net.cn

@ClassRule表示服务器在完成此类中的所有方法后关闭 已运行。spring-doc.cadn.net.cn

Rest 模板的松散 SSL 验证

WireMock 允许您使用httpsURL 协议。如果您的 应用程序想要在集成测试中联系该存根服务器,它发现 SSL 证书无效(自行安装的证书的常见问题)。 最好的选择通常是重新配置客户端以使用http.如果那不是 选项,你可以要求 Spring 配置一个忽略 SSL 验证错误的 HTTP 客户端 (当然,仅对测试执行此作)。spring-doc.cadn.net.cn

要轻松完成这项工作,您需要使用 Spring BootRestTemplateBuilder在您的应用程序中,如下例所示:spring-doc.cadn.net.cn

@Bean
public RestTemplate restTemplate(RestTemplateBuilder builder) {
	return builder.build();
}

你需要RestTemplateBuilder因为构建器是通过回调传递给 初始化它,以便可以在此时在客户端中设置 SSL 验证。这 在测试中自动发生,如果您使用@AutoConfigureWireMockannotation 或 Stub Runner 的 Oper 中。如果您使用 JUnit@Rule方法,您需要添加@AutoConfigureHttpClient注解,如下例所示:spring-doc.cadn.net.cn

@RunWith(SpringRunner.class)
@SpringBootTest("app.baseUrl=https://localhost:6443")
@AutoConfigureHttpClient
public class WiremockHttpsServerApplicationTests {

	@ClassRule
	public static WireMockClassRule wiremock = new WireMockClassRule(
			WireMockSpring.options().httpsPort(6443));
...
}

如果您使用spring-boot-starter-test,则您的 Apache HTTP 客户端位于 classpath 中,它由RestTemplateBuilder并配置为忽略 SSL 错误。如果使用默认的java.netclient,您不需要注解(但它 没有坏处)。目前不支持其他客户端,但可能会添加 在将来的版本中。spring-doc.cadn.net.cn

要禁用自定义RestTemplateBuilder中,将wiremock.rest-template-ssl-enabledproperty 设置为false.spring-doc.cadn.net.cn

WireMock 和 Spring MVC 模拟

Spring Cloud Contract 提供了一个方便的类,可以将 JSON WireMock 存根加载到 弹簧MockRestServiceServer.下面的项目表明了这一点。spring-doc.cadn.net.cn

baseUrlvalue 被添加到所有 mock 调用的前面,并且stubs()method 接受一个存根 path 资源模式作为参数。在前面的示例中,在/stubs/resource.json加载到 mock 服务器中。如果RestTemplate被要求 访问example.org/,它将响应获取在该 URL 中声明的响应。更多 可以指定一个存根模式,并且每个存根模式可以是一个目录(对于递归的 全部列表.json)、固定文件名(如前面的示例所示)或 Ant 样式 模式。JSON 格式是普通的 WireMock 格式,您可以在 WireMock 网站上阅读有关该格式的信息。spring-doc.cadn.net.cn

目前,Spring Cloud Contract Verifier 支持 Tomcat、Jetty 和 Undertow 作为 Spring Boot 嵌入式服务器,而 Wiremock 本身对特定的 Jetty 版本(当前为 9.2)。要使用本机 Jetty,您需要将本机 Wiremock 依赖项并排除 Spring Boot 容器(如果有)。spring-doc.cadn.net.cn