本文介绍了使用Wiremock时连接被拒绝的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Junit中有这段代码,我在其中明确将端口设置为8888

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(..

和错误:

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

推荐答案

来自 WireMock docs:

From the WireMock docs :

  1. 如果您使用Wiremock作为JUnit 4规则来配置端口,请使用:
import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.wireMockConfig;

...

@Rule
public WireMockRule wireMockRule = new WireMockRule(wireMockConfig().port(8888));
  1. 如果要从Test类(例如@Before)启动它:
  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. 使用默认实例的静态配置:
WireMock.configureFor(8888);

这篇关于使用Wiremock时连接被拒绝的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-29 10:41