【问题标题】:Pact Consumer Test Connection RefusedPact 消费者测试连接被拒绝
【发布时间】:2023-02-20 22:54:55
【问题描述】:

我正在尝试编写一个简单的 pact 测试,但是当我在 runTest 中调用端点时,连接总是被拒绝。看起来 PactProviderRuleMk2 无法正常工作。

public class ConsumerTestCurrencyExchange {

@Rule
public PactProviderRuleMk2 mockProvider = new PactProviderRuleMk2("forexService", "localhost", 8003, this);

@Pact(consumer = "forexServiceClient")
public RequestResponsePact createPact(PactDslWithProvider builder) {
    Map<String, String> headers = new HashMap<String, String>();
    headers.put("Content-Type", "application/json;charset=UTF-8");

    return builder
            .given("Test 1 For Currency Exchange")
                .uponReceiving("Pact JVM example Pact interaction")
                .path("/api/currency-exchange/from/USD/to/EUR")
                .method("GET")
                .headers(headers)
            .willRespondWith()
                .status(200)
                .headers(headers)
                .body(new PactDslJsonBody().stringType("conversionMultiple", "2").asBody())
            .toPact();
}

@Test
@PactVerification()
public void runTest() throws IOException, JSONException {
    System.setProperty("pact.rootDir", "../pacts");
    
    // Connection refused here
    HttpResponse response = Request.Get("http://localhost:8003/api/currency-exchange/from/USD/to/EUR").execute().returnResponse();
    
    
    String json = EntityUtils.toString(response.getEntity());
    JSONObject jsonObject = new JSONObject(json);
    String conversionMultiple = jsonObject.get("conversionMultiple").toString();

    assertEquals(conversionMultiple, "2");
}}

这是堆栈跟踪:

org.apache.http.conn.HttpHostConnectException: Connect to localhost:8003 [localhost/127.0.0.1, localhost/0:0:0:0:0:0:0:1] failed: Connection refused: connect
at org.apache.http.impl.conn.DefaultHttpClientConnectionOperator.connect(DefaultHttpClientConnectionOperator.java:156)
at org.apache.http.impl.conn.PoolingHttpClientConnectionManager.connect(PoolingHttpClientConnectionManager.java:376)
at org.apache.http.impl.execchain.MainClientExec.establishRoute(MainClientExec.java:393)
at org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:236)
at org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:186)
at org.apache.http.impl.execchain.RetryExec.execute(RetryExec.java:89)
at org.apache.http.impl.execchain.RedirectExec.execute(RedirectExec.java:110)
at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:185)
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:83)
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:56)
at org.apache.http.client.fluent.Request.internalExecute(Request.java:173)
at org.apache.http.client.fluent.Request.execute(Request.java:177)

依赖:(我尝试了不同的版本,但结果是一样的)

    <dependency>
        <groupId>au.com.dius</groupId>
        <artifactId>pact-jvm-consumer-junit_2.12</artifactId>
        <version>3.5.10</version>
        <scope>test</scope>
    </dependency>

【问题讨论】:

    标签: java pact


    【解决方案1】:

    遇到同样的问题,您找到解决方案了吗?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-26
      • 2020-03-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多