假设应用程序依赖于外部服务器上的REST服务http://otherserver.com。为了测试,我想在JUnit环境中模拟外部休息调用(通过Wiremock)。启动单独的服务器会消耗时间并且不容易。使用WiremockRule看起来是正确的方向。由于Wiremock可用,创建模拟控制器并不是一种优雅的方式。
例如。得到(“http://otherserver.com/service3/”);
PS:当然我知道我可以通过Mockito模拟REST调用。
使用Wiremock模拟localhost很容易。如何使用该代码模拟其他服务器和服务?我复制了流行的Baeldung例子中的部分内容。
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:<port>
,其中<port>
是你启动Wiremock服务器的地方(最好动态以避免端口冲突)
TL; DR:
你不能。
WireMock的作用是建立一个模拟您需要发送请求的远程服务器的Jetty服务器。但是,它不会更改您的hosts
文件或DNS映射,并自动将您对远程服务器的实际请求“重定向”到localhost。在测试中,您仍然需要向localhost
发送请求。
如果你使用Spring Boot,你可以做的是在application.yml
和main
包中创建两个test
文件(或另一个属性文件),具有相同的键结构,但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
我建议从urlEqualTo()
开始,不要搞乱正则表达式。然后你进入urlMatching()
。
另外,使用org.apache.http.util.EntityUtils
从响应中获取内容。这是处理响应的官方内置方式。并且,使用ResponseHandler
因为它将consume()
响应而无需手动清理资源。
查看HttpClient文档以获取更多详细信息。