Connection refused when using wiremock

*爱你&永不变心* 提交于 2021-02-07 14:53:53

问题


I have this piece of code in a Junit, where I clearly set the port to 8888

when(clientUtils.getLinkUrl(eq(HOSTELS_MICROSERVICE.name()), eq(HOSTELS_MICROSERVICE.name()), anyMap()))
                .thenReturn("http://localhost:8888/HOSTELS/HOSTELSMethods");

stubFor(com.github.tomakehurst.wiremock.client.WireMock.get("/HOSTELS/HOSTELS_LIST").willReturn(
                aResponse().withStatus(200)
                        .withHeader("Content-Type", APPLICATION_JSON_VALUE)
                        .withBody(ResourceUtils.getResourceFileAsString ("__files/HOSTELS.json"))));

but when I run the test I got this error on this line:

stubFor(com.github.tomakehurst.wiremock.client.WireMock.get("/HOSTELS/HOSTELS_LIST").willReturn(..

and the error:

wiremock.org.apache.http.conn.HttpHostConnectException: Connect to localhost:8080 [localhost/127.0.0.1, localhost/0:0:0:0:0:0:0:1] failed: Connection refused: connect

回答1:


From the WireMock docs :

  1. If you are using Wiremock as JUnit 4 rule to configure the port use :
import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.wireMockConfig;

...

@Rule
public WireMockRule wireMockRule = new WireMockRule(wireMockConfig().port(8888));
  1. If you are starting it from your Test class (for example @Before) :
import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.options;

...

WireMockServer wm = new WireMockServer(options().port(8888));

wm.stubFor(...)
  1. With static configuration of default instance :
WireMock.configureFor(8888);


来源:https://stackoverflow.com/questions/57626072/connection-refused-when-using-wiremock

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!