1

我是 WireMock 的新手,并试图让我的第一个单元测试使用它。现在,按照wiremock.org 上的文档,我写了这个

    WireMockConfiguration config = wireMockConfig().port(9089).httpsPort(8443);
    m_wireMockServer    =       new WireMockServer(config);

    m_wireMockServer.start();
    WireMock.configureFor("localhost", 9089);
    givenThat(get(urlEqualTo("/some/thing"))
    .willReturn(aResponse()
        .withHeader("Content-Type", "text/plain")
        .withBody("Hello world!")));

我希望这会向 /some/thing 发出任何 http 请求以被捕获。在给定的调用中,它给了我以下异常:

com.github.tomakehurst.wiremock.client.VerificationException: Expected status 201 for http://localhost:9089/__admin/mappings/new but was 200
at com.github.tomakehurst.wiremock.client.HttpAdminClient.postJsonAssertOkAndReturnBody(HttpAdminClient.java:151)
at com.github.tomakehurst.wiremock.client.HttpAdminClient.addStubMapping(HttpAdminClient.java:65)
at com.github.tomakehurst.wiremock.client.WireMock.register(WireMock.java:130)
at com.github.tomakehurst.wiremock.client.WireMock.register(WireMock.java:126)
at com.github.tomakehurst.wiremock.client.WireMock.givenThat(WireMock.java:65)

我错过了什么?创建存根有什么问题?

4

1 回答 1

2

我的猜测是,您在测试运行之间一直在后台运行 Wiremock,因此当您在测试中设置新映射时,映射并不是真正的新映射,因为已经存在一个并且 Wiremock 响应 200(OK ) 而不是 201(已创建)。

为了验证这个假设,尝试在分配给 Wiremock 的端口上定位进程,如果有,将其杀死并再次运行测试。

于 2015-11-10T15:59:07.570 回答