Java Wiremock-测试期间模拟外部服务器
假设应用程序依赖于外部服务器上的REST服务。为了进行测试,我想在JUnit环境中模拟外部rest调用(通过Wiremock)。启动一个单独的服务器需要花费时间,而且不容易。使用WiremockRule看起来方向正确。创建模拟控制器并不是一种优雅的方式,因为Wiremock是可用的 例如,获取(“”) PS:当然我知道我可以通过Mockito模拟REST调用 使用Wiremock模拟本地主机很容易。如何使用该代码模拟其他服务器和服务?我从流行的贝尔东例子中复制了一些部分Java Wiremock-测试期间模拟外部服务器,java,wiremock,Java,Wiremock,假设应用程序依赖于外部服务器上的REST服务。为了进行测试,我想在JUnit环境中模拟外部rest调用(通过Wiremock)。启动一个单独的服务器需要花费时间,而且不容易。使用WiremockRule看起来方向正确。创建模拟控制器并不是一种优雅的方式,因为Wiremock是可用的 例如,获取(“”) PS:当然我知道我可以通过Mockito模拟REST调用 使用Wiremock模拟本地主机很容易。如何使用该代码模拟其他服务器和服务?我从流行的贝尔东例子中复制了一些部分 public class
public class WireMockDemo {
@Rule
public WireMockRule wireMockRule = new WireMockRule();
@Test
public void wireMockTestJunitOtherServer() {
try {
// **this does not work...**
configureFor("otherserver.com", 8080);
stubFor(get(urlPathMatching("/service2/.*"))
.willReturn(aResponse()
.withStatus(200)
.withHeader("Content-Type", "application/json")
.withBody("\"testing-library\": \"WireMock\"")));
// Test via simple client
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpGet request = new HttpGet("http://otherserver:8080/service2/test");
HttpResponse httpResponse = httpClient.execute(request);
String stringResponse = convertHttpResponseToString(httpResponse);
System.out.println( "Response = " + stringResponse);
// Test via JUnit
verify(getRequestedFor(urlEqualTo("/service2/wiremock")));
assertEquals(200, httpResponse.getStatusLine().getStatusCode());
assertEquals("application/json", httpResponse.getFirstHeader("Content-Type").getValue());
assertEquals("\"testing-library\": \"WireMock\"", stringResponse);
} catch( Exception e) {
e.printStackTrace();
}
}
// Support methods
private String convertHttpResponseToString(HttpResponse httpResponse) throws IOException {
InputStream inputStream = httpResponse.getEntity().getContent();
return convertInputStreamToString(inputStream);
}
private String convertInputStreamToString(InputStream inputStream) {
Scanner scanner = new Scanner(inputStream, "UTF-8");
String string = scanner.useDelimiter("\\Z").next();
scanner.close();
return string;
}
}
您的应用程序代码不应包含
http://otherserver.com
硬编码,应可配置。正常运行时,应指向http://otherserver.com
,在测试模式下运行时,应指向http://localhost:
其中
是您启动Wiremock服务器的地方(最好是动态的,以避免端口冲突)TL;博士:
不,你不能
WireMock所做的是建立一个Jetty服务器,模拟您需要向其发送请求的远程服务器。但是,它不会更改您的主机
文件或DNS映射,并自动将您对远程服务器的实际请求“重定向”到本地主机。在测试中,您仍然需要将请求发送到localhost
如果您使用Spring Boot,您可以做的是在main
和test
包中创建两个application.yml
文件(或另一个属性文件),使用相同的键结构,但src/main/resources/application.yml
中的值是您请求的真实URL(如http://example.com/api
),在src/test/resources/application.yml
中放置localhost/api
顺便说一句,为了澄清,
MockMvc
不是用于模拟应用程序所依赖的外部第三方服务器请求,而是用于模拟发送到应用程序端点的请求。在MockMvc测试中,您的应用程序接收请求,但在WireMock测试中,您的应用程序发送请求
一些工作示例:
// configure static, class-level rule for all tests, like @BeforeClass.
// this launches a Jetty server with configurations
@ClassRule
public static WireMockClassRule classRule = new WireMockClassRule(options().
port(80).httpsPort(443));
// Divide @ClassRule and @Rule,
// to bypass JUnit limitation of "@Rule cannot be static"
@Rule
public WireMockClassRule rule = classRule;
@Test
public void shouldReturnGivenJson() throws Exception {
// stubFor() also works; givenThat() is more TDD-ish
givenThat(post(urlEqualTo("/service2/test")) // <----- note here: without host name
.willReturn(WireMock.aResponse()
.withStatus(HttpStatus.OK.value())
.withHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_UTF8_VALUE)
.withBody("{\"status\":\"up\"}")));
// .... your connection here
//为所有测试配置静态类级规则,如@BeforeClass。
//这将启动带有配置的Jetty服务器
@阶级规则
公共静态WireMockClassRule classRule=新的WireMockClassRule(选项()。
港口(80.httpsPort(443));
//将@ClassRule和@Rule分开,
//绕过JUnit限制“@Rule不能是静态的”
@统治
public WireMockClassRule rule=classRule;
@试验
public void shouldReturnGivenJson()引发异常{
//stubFor()也可以工作;givenThat()更像TDD
givenThat(post(urlEqualTo(“/service2/test”))//