基本pact/junit5测试设置失败对于提供程序错误,找不到用@pact注解的方法

yqhsw0fo  于 2021-07-24  发布在  Java
关注(0)|答案(1)|浏览(468)

我试图按照pact.io上的文档编写一个简单的集成测试。不幸的是,我得到一个例外如下:

org.junit.jupiter.api.extension.ParameterResolutionException: Failed to resolve parameter [au.com.dius.pact.consumer.MockServer mockServer] in method [public void com.example.demo.integration.pact.PactTest.setUp(au.com.dius.pact.consumer.MockServer)]: No method annotated with @Pact was found on test class PactTest for provider 'node_server'

它说我没有用@pact注解任何方法。不过,我有一个方法,用@pact注解。
我试着手动运行这个测试,并使用“mvn test”。
一般来说,应用程序提供了一些rest控制器,应该进行测试。
下面是关于我的pact测试实现的所有实现。我错过什么了吗?

package com.example.demo.integration.pact;

import au.com.dius.pact.consumer.MockServer;
import au.com.dius.pact.consumer.dsl.PactDslWithProvider;
import au.com.dius.pact.consumer.junit5.PactConsumerTestExt;
import au.com.dius.pact.consumer.junit5.PactTestFor;
import au.com.dius.pact.core.model.RequestResponsePact;
import au.com.dius.pact.core.model.annotations.Pact;
import org.apache.http.HttpResponse;
import org.apache.http.client.fluent.Request;
import org.json.JSONArray;
import org.json.JSONObject;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;

import java.io.IOException;

import static org.junit.jupiter.api.Assertions.assertEquals;

@ExtendWith(PactConsumerTestExt.class)
@PactTestFor(providerName = PactTest.PACT_PROVIDER_NAME)
public class PactTest {

    public static final String PACT_PROVIDER_NAME = "node_server";

    public static final String PACT_CONSUMER_NAME = "spring_application";

    @BeforeEach
    public void setUp(MockServer mockServer) {
        System.out.println("Mockserver check called");
        Assertions.assertTrue(mockServer != null);
    }

    @Pact(provider = PACT_PROVIDER_NAME, consumer = PACT_CONSUMER_NAME)
    public RequestResponsePact createPact(PactDslWithProvider builder) {

        return builder
                .uponReceiving("notes")
                .path("/notes")
                .method("GET")
                .willRespondWith()
                .matchHeader("Content-Type","application/json")
                .status(200)
                .body(
                        getJsonArrayOfNotes(2).toString())
                .toPact();
    }

    @Test
    @PactTestFor(pactMethod = "notes")
    void test(MockServer mockServer) throws IOException {
        HttpResponse httpResponse = Request.Get(mockServer.getUrl() + "/notes").execute().returnResponse();
        assertEquals(200, httpResponse.getStatusLine().getStatusCode());
        assertEquals(getJsonArrayOfNotes(2).toString(),httpResponse.getEntity().getContent().toString());
    }

    private JSONArray getJsonArrayOfNotes(int size) {
        var responseJsonObject = new JSONArray();
        for (int i = 0; i < size; i++) {
            var note = new JSONObject();
            try {
                note.put("title", String.format("Title %s", i + 1));
                note.put("content", String.format("Some Note Content of Note %s", i + 1));
            } catch (Exception exception) {

            }
            responseJsonObject.put(note);
        }
        return responseJsonObject;
    }

}
k0pti3hp

k0pti3hp1#

似乎带有@pact注解的方法名必须与@pacttsetfor注解中的pactmethod相同。。。
就我而言,我必须写下:

@Test
@PactTestFor(pactMethod = "getNotes")
void test(MockServer mockServer) throws IOException {
        HttpResponse httpResponse = Request.Get(mockServer.getUrl() + "/notes").execute().returnResponse();
        assertEquals(200, httpResponse.getStatusLine().getStatusCode());
        assertEquals(getJsonArrayOfNotes(2).toString(),httpResponse.getEntity().getContent().toString());
    }

@Pact(provider = PACT_PROVIDER_NAME, consumer = PACT_CONSUMER_NAME)
public RequestResponsePact getNotes(PactDslWithProvider builder) {

        return builder
                .uponReceiving("notes")
                .path("/notes")
                .method("GET")
                .willRespondWith()
                .matchHeader("Content-Type","application/json")
                .status(200)
                .body(
                        getJsonArrayOfNotes(2).toString())
                .toPact();
    }

相关问题