【问题标题】:HttpHostConnectException when initializing WireMock HTTP server初始化 WireMock HTTP 服务器时出现 HttpHostConnectException
【发布时间】:2016-03-23 20:40:15
【问题描述】:

我正在尝试测试对 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 开头,因此这可能是其内部配置的问题。

这里有什么问题?

【问题讨论】:

    标签: wiremock httphostconnectexception


    【解决方案1】:

    这里的问题是@BeforeClass@rule 之前执行。 将存根移入@Before 方法或直接移入测试可以解决问题。

    【讨论】:

    • 我遇到了与上述相同的问题。但是,this 并没有解决我的问题。它仍在尝试在同一端口 8080 上旋转服务器。虽然,指定的端口是 8888。任何人都有一个想法。我的代码与Getting Started 指南中的代码完全相同。
    猜你喜欢
    • 1970-01-01
    • 2013-07-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-21
    • 2018-11-19
    • 2013-01-28
    相关资源
    最近更新 更多