1

执行 PACT JVM 测试用例时,出现以下错误。实际上,在此错误之前,还有另一个 scala 错误,在添加一个 scala 依赖项后,该问题已解决,但出现了这个。

    java.lang.NoClassDefFoundError: scala/Product$class
    at au.com.dius.pact.model.RequestMatching.<init>(RequestMatching.scala:7)
    at au.com.dius.pact.model.RequestMatching$.apply(RequestMatching.scala:7)
    at au.com.dius.pact.model.RequestMatching.apply(RequestMatching.scala)
    at au.com.dius.pact.consumer.BaseMockServer.<init>(MockHttpServer.kt:61)
    at au.com.dius.pact.consumer.MockHttpServer.<init>(MockHttpServer.kt:202)
    at au.com.dius.pact.consumer.MockHttpServerKt.mockServer(MockHttpServer.kt:34)
    at au.com.dius.pact.consumer.ConsumerPactRunnerKt.runConsumerTest(ConsumerPactRunner.kt:12)
    at au.com.dius.pact.consumer.ConsumerPactTestMk2.testPact(ConsumerPactTestMk2.java:26)

这是我的代码:`public class ContractTest extends ConsumerPactTestMk2 {

    @Rule
    public PactProviderRuleMk2 mockProvider = new PactProviderRuleMk2("test_provider", "localhost", 5055, this);

    @Override
    @Pact(provider="test_provider", consumer="test_consumer")
    protected RequestResponsePact createPact(PactDslWithProvider builder) {
        Map<String, String> headers = new HashMap<String, String>();
        headers.put("testreqheader", "testreqheadervalue");

        JSONObject updateJsonObj = new JSONObject();
        JSONObject updateVersion = new JSONObject();
        updateVersion.put("version", "1.4");
        updateJsonObj.put("update", updateVersion);
        return builder.given("test state 1") // NOTE: Using provider states are optional, you can leave it out
                .uponReceiving("ExampleJavaConsumerPactTest test interaction")
                    .path("/hostService/hosts/1")
                    .method("PUT")
                    .body(updateJsonObj)
                .willRespondWith()
                    .status(200)
                    .body(updateJsonObj)
                .given("test state 2") // NOTE: Using provider states are optional, you can leave it out
                .uponReceiving("ExampleJavaConsumerPactTest second test interaction")
                    .method("GET")
                    .path("/hostService/hosts/1")
                    .body("")
                .willRespondWith()
                    .status(200)
                    .body("")
                .toPact();
    }

    @Override
    protected String providerName() {
        return "test_provider";
    }

    @Override
    protected String consumerName() {
        return "test_consumer";
    }

    @Override
    protected void runTest(MockServer mockServer) throws IOException {
        HttpClient httpClient = HttpClients.createDefault();        
        HttpPut putReq = new HttpPut(mockServer.getUrl());
        HttpGet getReq = new HttpGet(mockServer.getUrl());

        HttpResponse response = httpClient.execute(getReq);     
        Assert.assertEquals(response.getStatusLine().getStatusCode(), 200);

        response = httpClient.execute(putReq);
        Assert.assertEquals(getVersion(response.getEntity().toString()), "1.3"); 
    }

`

4

1 回答 1

1

看起来 3.5.5 版本的依赖关系有点坏。目前,您有两个选择:

  1. 通过降级到版本 3.5.4 来解决它

  2. 明确包括缺少的依赖项。对我来说,这是:

于 2017-10-02T00:52:25.580 回答