介绍
WireMock是用于基于HTTP的API的模拟服务器。 有些人可能会将其视为服务虚拟化工具或模拟服务器。 它使您能够存根所需的API或其他外部依赖项,以加快本地开发速度。 它支持测试真实API无法可靠产生的极端情况和失败模式。 在模拟单元测试和集成测试中的外部依赖关系时,它也很有用。 它与jUnit具有出色的集成。
添加Wiremock依赖性
首先,您将要添加Wiremock依赖项。 您可以下载常规依赖项或包含所有依赖项的胖JAR独立版本。 我们将在此处使用标准依赖项。 将以下依赖项添加到您的build.gradle中
build.gradle
dependencies {testCompile('com.github.tomakehurst:wiremock:2.1.12')
}
添加Wiremock单元测试
这是完整的单元测试,可用于测试与Wiremock的集成。 该单元测试使用jUnit4规则启动端口8089上的Wiremock服务器,并在每次测试后将其关闭。 我们使用stubFor方法来定义模拟的终点和响应。 我们使用Spring RestTemplate创建一个到模拟服务器的HTTP请求并捕获结果。
WiremockTests.java
public class WiremockTests {RestTemplate restTemplate;ResponseEntity response;@Rulepublic WireMockRule wireMockRule = new WireMockRule(wireMockConfig().port(8089).httpsPort(8443));@Beforepublic void setup() throws Exception {restTemplate = new RestTemplate();response = null;}@Testpublic void givenWireMockAdminEndpoint_whenGetWithoutParams_thenVerifyRequest() {RestTemplate restTemplate = new RestTemplate();response = restTemplate.getForEntity("http://localhost:8089/__admin", String.class);assertThat("Verify Response Body", response.getBody().contains("mappings"));assertThat("Verify Status Code", response.getStatusCode().equals(HttpStatus.OK));}@Testpublic void givenWireMockEndpoint_whenGetWithoutParams_thenVerifyRequest() {stubFor(get(urlEqualTo("/api/resource/")).willReturn(aResponse().withStatus(HttpStatus.OK.value()).withHeader("Content-Type", TEXT_PLAIN_VALUE).withBody("test")));response = restTemplate.getForEntity("http://localhost:8089/api/resource/", String.class);assertThat("Verify Response Body", response.getBody().contains("test"));assertThat("Verify Status Code", response.getStatusCode().equals(HttpStatus.OK));verify(getRequestedFor(urlMatching("/api/resource/.*")));}
}
您可以运行此测试,如果完成,则您已成功将Wiremock集成到应用程序中。
深入到单元测试
这是一些静态导入,您可以在测试中使用这些静态导入来提高可读性和简洁性。
WiremockTests.java
import static com.github.tomakehurst.wiremock.client.WireMock.*;
import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.wireMockConfig;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.springframework.http.MediaType.TEXT_PLAIN_VALUE;
jUnit4规则
这个jUnit4 @rule将自动管理Wiremock服务器的生命周期以及每个测试用例的启动和关闭Wiremock。 您也可以使用setup()和teardown()方法来实现,但是jUnit4规则更加简洁明了。
WiremockTests.java
@Rulepublic WireMockRule wireMockRule = new WireMockRule(wireMockConfig().port(8089).httpsPort(8443));
残端和响应
此代码使用stubFor()方法(已静态导入)定义端点, / api / resource /和纯文本响应主体“ test”。您也可以使用此方法通过更改Content-返回JSON或XML响应。类型和响应主体
WiremockTests.java
stubFor(get(urlEqualTo("/api/resource/")).willReturn(aResponse().withStatus(HttpStatus.OK.value()).withHeader("Content-Type", TEXT_PLAIN_VALUE).withBody("test")));
春天RestTemplate
我们使用Spring RestTemplate类对http:// localhost:8089 / api / resource /执行GET HTTP请求,以打到Wiremock服务器的存根端点。 在这种情况下,我们期望一个String.class实体响应,因为那是我们在stubFor()方法中定义的。 如果您配置了POJO对象,则需要定义一个POJO对象以从存根方法捕获JSON响应。 我们在ResponseEntity对象中捕获响应,该对象捕获响应主体,标头和状态代码以及有关请求的其他信息。
WiremockTests.java
response = restTemplate.getForEntity("http://localhost:8089/api/resource/", String.class);
手动启动和停止Wiremock服务器
您可以手动启动和停止Wiremock服务器,而无需使用jUnit4规则来管理生命周期。 当您的应用程序启动时,您可能希望这样做是一种引导方法。
ServiceClass.java
WireMockServer wireMockServer = new WireMockServer(wireMockConfig().port(8089)); //No-args constructor will start on port 8080, no HTTPS
wireMockServer.start();WireMock.reset();wireMockServer.stop();
结论
现在,您的项目中已安装Wiremock。 您可以在单元测试和集成测试中使用Wiremock来对外部依赖关系进行存根,也可以加快本地环境中的开发。 您可以在此处阅读有关设置Wiremock的更多信息: http ://wiremock.org/docs/getting-started/
翻译自: https://www.javacodegeeks.com/2016/09/integrate-wiremock-spring-boot-java-web-application-mocking-external-dependencies.html