gpt4 book ai didi

wiremock - 初始化 WireMock HTTP 服务器时出现 HttpHostConnectException

转载 作者:行者123 更新时间:2023-12-04 15:20:21 27 4
gpt4 key购买 nike

我正在尝试测试我的代码,该代码具有对 HTTP 服务器的底层 GET 调用。

我正在尝试使用 WireMock ,并基于“入门”我有以下代码:

@Rule
public WireMockRule wireMockRule =
new WireMockRule(WireMockConfiguration.wireMockConfig().port(8888)); // No-args constructor defaults to port 8080

@BeforeClass
public static void beforeClass(){
stubFor(get(urlEqualTo("/Path/Get"))
.willReturn(aResponse()
.withStatus(200)
.withBody("[1]")));
}

@Test
public void testGetRanking() throws Exception {
Fetcher fetcher = new Fetcher("http://localhost:8888",null);
int rankinganking = fetcher.getRanking("Max");
Assert.assertEquals(1, ranking);
}

当我运行测试时,我得到以下堆栈跟踪:
wiremock.org.apache.http.conn.HttpHostConnectException: Connect to localhost:8080 [localhost/127.0.0.1] failed: Connection refused
at wiremock.org.apache.http.impl.conn.DefaultHttpClientConnectionOperator.connect(DefaultHttpClientConnectionOperator.java:151)
at wiremock.org.apache.http.impl.conn.PoolingHttpClientConnectionManager.connect(PoolingHttpClientConnectionManager.java:353)
at wiremock.org.apache.http.impl.execchain.MainClientExec.establishRoute(MainClientExec.java:380)
at wiremock.org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:236)
...

该错误涉及端口 8080,但我将端口配置为 8888。我知道默认情况下 WireMock 以端口 8080 开头,因此这可能是其内部配置的问题。

这里有什么问题?

最佳答案

这里的问题是 @BeforeClass@rule 之前执行.
将 stub 移入 @Before方法或直接进入测试解决问题。

关于wiremock - 初始化 WireMock HTTP 服务器时出现 HttpHostConnectException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34333570/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com