Java Wiremock - 在测试期间模拟外部服务器

时间:2018-04-18 17:33:08

标签: java wiremock

假设应用程序依赖于外部服务器上的REST服务,http://otherserver.com。为了测试,我想在JUnit环境中模拟外部休息调用(通过Wiremock)。启动单独的服务器会消耗时间并且不容易。使用WiremockRule看起来是正确的方向。由于Wiremock可用,创建模拟控制器并不是一种优雅的方式。

E.g。得到(“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;
    }
}

2 个答案:

答案 0 :(得分:2)

您的应用程序代码不应具有http://otherserver.com硬编码,应该是可配置的。正常运行时应指向http://otherserver.com,在测试模式下运行时,应指向http://localhost:<port>,其中<port>是启动Wiremock服务器的位置(最好动态以避免端口冲突)

答案 1 :(得分:0)

TL; DR:

不,你不能。

WireMock的作用是建立一个Jetty服务器,模拟您需要向其发送请求的远程服务器。但是,它不会更改您的hosts文件或DNS映射,并且会自动将您对远程服务器的实际请求“重定向”到localhost。在测试中,您仍然需要向localhost发送请求。

如果使用的是Spring Boot,您可以做的是在application.ymlmain包中创建两个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文档。