【发布时间】:2015-04-28 14:07:34
【问题描述】:
我是 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)
我错过了什么?创建存根有什么问题?
【问题讨论】:
-
这里先从 URL 的区别说起。
get方法调用中的 URL 与断言失败的 URL 不同。 -
是的,我想使用 /some/thing 的模拟行为的测试甚至没有从这里开始。我希望对 /some/thing 的请求表现得像为我的测试定义的那样。但是在注册此行为时会发生异常。注册返回200,但我理解的需要201。
标签: java unit-testing wiremock