我正在尝试为Wiremck启用响应模板。留档指出需要添加以下代码:
@Rule
public WireMockRule wm = new WireMockRule(options()
.extensions(new ResponseTemplateTransformer(false))
);
但是,这个@Law注释适用于JUnit,目前,我没有在jUnit测试中运行wiremck。那么,有没有办法在应用程序范围内启用WireMockLaw?
要在Spring Boot应用程序中启用WireMock,您可以使用wiremck-jre8依赖项中的WireMockLaw类。以下是步骤:
<!-- Maven -->
<dependency>
<groupId>com.github.tomakehurst</groupId>
<artifactId>wiremock-jre8</artifactId>
<version>2.27.2</version>
<scope>test</scope>
</dependency>
或build. gradle
testImplementation 'com.github.tomakehurst:wiremock-jre8:2.27.2'
@Rule
public WireMockRule wireMockRule = new WireMockRule(options().port(8080));
@Test
public void testMyService() {
stubFor(get(urlEqualTo("/my/resource"))
.willReturn(aResponse()
.withStatus(200)
.withBody("Hello, world!")));
// make a call to your service that depends on the external service
// and verify that it behaves as expected
}
请注意,步骤3中的option(). port(8080)方法调用指定了WireMock将在其上运行的端口。您可以将其更改为您喜欢的任何可用端口。