【发布时间】:2020-01-29 19:15:54
【问题描述】:
@Test
void resourceTo_Wired_ClientCall() {
wireMockServer = new WireMockServer(options().port(10021));
wireMockServer.start();
final MappingBuilder stubBuilder = get(urlMatching("http://demo1096495.mockable.io/client1"));
stubFor(stubBuilder
.willReturn(aResponse()
.withStatus(200)
.withHeader("Content-Type", "application/json")
.withBody("{\"something\":\"test-wired\"}")));
;
出现错误:
org.apache.http.conn.HttpHostConnectException:连接到 localhost:8080 [localhost/127.0.0.1] 失败:连接被拒绝 (连接被拒绝)
遗漏了什么?
附:我使用WireMockServer 和junit 5 和弹簧靴。 junit 4 和 WireMockRule 都运行良好。
testCompile "com.github.tomakehurst:wiremock-jre8:2.24.1"
【问题讨论】:
-
在实际测试中你从哪里得到错误?您只发布了 Wiremock 设置。如果您的端口是 10021,为什么测试会尝试连接到 :8080?
-
它发生在 stubFor(..) 上,为什么实际上是这个问题 :) (虽然使用 junit4 和 WireMockRule 配置效果很好)
-
旁注:由于您使用
stubFor作为静态方法 - 您配置默认使用8080端口的默认wiremock 实例。如果要为在测试中创建的实例创建存根,则应在实例上使用wireMockServer.stubFor。