wiremock存根创建失败:预期201,但得到200

hsvhsicv  于 2021-07-13  发布在  Java
关注(0)|答案(1)|浏览(329)

我是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)

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

k2arahey

k2arahey1#

我的猜测是,在测试运行之间,wiremock一直在后台运行,因此当您在test中设置一个新的Map时,Map并不是真正的新Map,因为已经存在一个Map,wiremock响应200(确定)而不是201(已创建)。
为了验证这个假设,请尝试在分配给wiremock的端口上定位进程,如果有进程,请杀死它并再次运行测试。

相关问题