WireMock存根创建失败:期望201但获得200

时间:2015-04-28 14:07:35

标签: java unit-testing wiremock

我是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请求。它给出了以下关于giventhat调用的例外:

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)

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

1 个答案:

答案 0 :(得分:2)

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

要测试此假设,请尝试在分配给Wiremock的端口上找到进程,如果有,请将其终止并再次运行测试。