由于NoHttpResponseException,Pact文件创建失败:127.0。*。*。***无法响应

时间:2017-07-06 02:45:24

标签: api unit-testing pact pact-jvm

我无法创建Pact文件。 面对NoHttpResponseException:127.0.0.1:56314无法响应。 我能够成功生成pact文件,但在我做了Maven-> clean之后我面临一系列问题。 请告诉我原因是什么以及如何解决?

我的POM文件:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>Customer</groupId>
  <artifactId>Auth_Api_Consumer</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>Auth_Api_Consumer</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <dependencies>
        <dependency>
            <groupId>au.com.dius</groupId>
            <artifactId>pact-jvm-consumer-junit_2.11</artifactId>
            <version>3.4.0</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.9</version>
        </dependency>
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>fluent-hc</artifactId>
            <version>4.5.2</version>
        </dependency>

        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-collections4</artifactId>
            <version>4.0</version>
        </dependency>


    </dependencies>
</project>

我的契约测试文件:

package Customer.Auth_Api_Consumer;

import java.io.IOException;

import org.apache.http.entity.ContentType;
import org.junit.Rule;
import org.junit.Test;
import Utilities.ApplicationConstants;
import au.com.dius.pact.consumer.Pact;
import au.com.dius.pact.consumer.PactProviderRuleMk2;
import au.com.dius.pact.consumer.PactVerification;
import au.com.dius.pact.consumer.dsl.PactDslWithProvider;
import au.com.dius.pact.model.RequestResponsePact;
import junit.framework.Assert;

public class Authorisation_API_Test {
    @Rule
    public PactProviderRuleMk2 provider = new PactProviderRuleMk2(ApplicationConstants.Provider_EcCustomer, this);

    @Pact(provider = ApplicationConstants.Provider_EcCustomer, consumer = ApplicationConstants.Consumer_EcCart)
    public RequestResponsePact createFragment(PactDslWithProvider builder) {

        return builder.given("Provider State:Verify that the user Vijay exists")
                .uponReceiving("Verify Valid User Login").path("/hue-ec-customer/v1/customers/userlogin/verify")
                .method("POST")
                .body("{\"customerGroupId\": \"3ee05990-5022-11e7-a2ea-e7a96dab751e\", \"login\": \"vijay\", \"password\": \"testing\" }")
                .willRespondWith().status(200)
                .given("Provider State:Verify that the user Vijay exists")
                .uponReceiving("Verify Invalid User Login").path("/hue-ec-customer/v1/customers/userlogin/verify")
                .method("POST")
                .body("{\"customerGroupId\": \"3ee05990-5022-11e7-a2ea-e7a96dab751e\", \"login\": \"vijay\", \"password\": \"123456\" }")
                .willRespondWith().status(401).
                uponReceiving("Create User")
                .path("hue-ec-customer/v1/customers/userlogin/create").method("POST")
                .body("{\"customerGroupId\": \"3ee05990-5022-11e7-a2ea-e7a96dab751e\","
                        + " \"userId\": \"tehuser\", \"loginEmailAddress\":" + " \"teh.sm@mail.com\" "
                        + " \"loginPhoneNumber\": \"8934234\"" + " \"password\": \"defaultpassword\"}")
                .willRespondWith().status(201)
                .toPact();
    }

    @Test
    @PactVerification(ApplicationConstants.Provider_EcCustomer)
    public void runTest() throws IOException {
        System.out.println("Mock Server started at : " + provider.getUrl());
        System.out.println(new Client(provider.getUrl()).postBody("/hue-ec-customer/v1/customers/userlogin/verify",
                        "{\"customerGroupId\": \"3ee05990-5022-11e7-a2ea-e7a96dab751e\","
                                + " \"login\": \"vijay\", \"password\": \"testing\" }",
                        ContentType.APPLICATION_JSON)
                .getStatusLine().getStatusCode());
        Assert.assertEquals(200, new Client(provider.getUrl())
                .postBody("/hue-ec-customer/v1/customers/userlogin/verify",
                        "{\"customerGroupId\": \"3ee05990-5022-11e7-a2ea-e7a96dab751e\","
                                + " \"login\": \"vijay\", \"password\": \"testing\" }",
                        ContentType.APPLICATION_JSON)
                .getStatusLine().getStatusCode());
        Assert.assertEquals(new Client(provider.getUrl())
                .postBody("/hue-ec-customer/v1/customers/userlogin/verify",
                        "{\"customerGroupId\": \"3ee05990-5022-11e7-a2ea-e7a96dab751e\","
                                + " \"login\": \"vijay\", \"password\": \"123456\" }",
                        ContentType.APPLICATION_JSON)
                .getStatusLine().getStatusCode(), 401);

        Assert.assertEquals(
                new Client(provider.getUrl()).postBody("/hue-ec-customer/v1/customers/userlogin/create",
                        "{\"customerGroupId\": \"3ee05990-5022-11e7-a2ea-e7a96dab751e\","
                                + " \"userId\": \"tehuser\", \"loginEmailAddress\":" + " \"teh.sm@mail.com\" "
                                + " \"loginPhoneNumber\": \"8934234\"" + " \"password\": \"defaultpassword\"}",
                        ContentType.APPLICATION_JSON).getStatusLine().getStatusCode(),
                201);


    }
}

1 个答案:

答案 0 :(得分:0)

我不得不将Junit版本降级到4.9