简体   繁体   English

无法运行 JUnit5 PACT 测试。 在提供程序 '' 的测试 class ConsumerContractTest 中找不到使用 @Pact 注释的方法

[英]Unable to run JUnit5 PACT test. No method annotated with @Pact was found on test class ConsumerContractTest for provider ''

I'm trying to get a PACT test running on JUnit5.我正在尝试在 JUnit5 上运行 PACT 测试。 We use JUnit4 for others, but this one will be JUnit5.我们为其他人使用 JUnit4,但这个将是 JUnit5。 The error occurs when running the JUnit5 test using the pact annotation on the RequestResponsePact method.RequestResponsePact方法上使用 pact 注释运行 JUnit5 测试时会发生错误。

Error: No method annotated with @Pact was found on test class ConsumerContractTest for provider '' .错误: No method annotated with @Pact was found on test class ConsumerContractTest for provider ''

I've seen Basic Pact/Junit5 Test Setup fails.我见过Basic Pact/Junit5 测试设置失败。 No method annotated with @Pact was found for provider error , but this is issue was due to the @PactTestFor(pactMethod = "examplePact") not matching the @Pact method name. No method with @Pact was found for provider error ,但这是由于@PactTestFor(pactMethod = "examplePact")@Pact方法名称不匹配而导致的问题。 But on my code it does match.但在我的代码中它确实匹配。

I can't seem to figure out why I get the error and especially why the error has an empty provider( provider '' ) despite defining one( "some-provider" ).我似乎无法弄清楚为什么我会收到错误,尤其是为什么错误有一个空的提供者( provider '' )尽管定义了一个( "some-provider" )。

Example code:示例代码:

import au.com.dius.pact.consumer.MockServer
import au.com.dius.pact.consumer.Pact
import au.com.dius.pact.consumer.dsl.PactDslJsonArray
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.model.RequestResponsePact
import groovyx.net.http.RESTClient
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.extension.ExtendWith
import org.springframework.http.HttpStatus


@ExtendWith(PactConsumerTestExt.class)
class ConsumerContractTest {


    @Pact(consumer = "some-consumer", provider = "some-provider")
    RequestResponsePact examplePact(PactDslWithProvider builder) {

        builder
                .given("provider state")
                .uponReceiving("Contract description")
                .method("GET")
                .matchPath("/endpoint")
                .willRespondWith()
                .status(200)
                .headers(["Content-Type": "application/vnd.pnf.v1+json"])
                .body(new PactDslJsonArray())
                .toPact()
    }

    @Test
    @PactTestFor(pactMethod = "examplePact")
    void exampleTest(MockServer mockServer) {
        def client = new RESTClient(mockServer.getUrl())
    }
}

Not sure if that's just the gist you've posted here but I see the return word missing and also the @PactTestFor annotation missing the provider and version.不确定这是否只是您在此处发布的要点,但我看到缺少return词以及缺少提供程序和版本的@PactTestFor注释。 Here is an example I have that works for my project.这是我有一个适用于我的项目的示例。

import au.com.dius.pact.consumer.dsl.DslPart;
import au.com.dius.pact.consumer.dsl.PactDslJsonBody;
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.PactSpecVersion;
import au.com.dius.pact.core.model.RequestResponsePact;
import au.com.dius.pact.core.model.annotations.Pact;
import io.restassured.response.Response;
import io.restassured.specification.RequestSpecification;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;

import java.util.HashMap;
import java.util.Map;

import static com.example.mbbackend.config.Constants.*;
import static com.example.mbbackend.util.Utils.getRequestSpecification;
import static org.junit.jupiter.api.Assertions.assertEquals;

@ExtendWith(PactConsumerTestExt.class)
class GetActorIT {

    Map<String, String> headers = new HashMap<>();

    String path = "/api/mb/actor/";

    @Pact(provider = PACT_PROVIDER, consumer = PACT_CONSUMER)
    public RequestResponsePact createPact(PactDslWithProvider builder) {

        headers.put("Content-Type", "application/json");

        DslPart bodyReturned = new PactDslJsonBody()
                .uuid("id", "1bfff94a-b70e-4b39-bd2a-be1c0f898589")
                .stringType("name", "A name")
                .stringType("family", "A family")
                .stringType("imageUrl", "http://anyimage.com")
                .close();

        return builder
                .given("A request to retrieve an actor")
                .uponReceiving("A request to retrieve an actor")
                .pathFromProviderState(path + "${actorId}", path + "1bfff94a-b70e-4b39-bd2a-be1c0f898589")
                .method("GET")
                .headers(headers)
                .willRespondWith()
                .status(200)
                .body(bodyReturned)
                .toPact();
    }

    @Test
    @PactTestFor(providerName = PACT_PROVIDER, port = PACT_PORT, pactVersion = PactSpecVersion.V3)
    void runTest() {

        //Mock url
        RequestSpecification rq = getRequestSpecification().baseUri(MOCK_PACT_URL).headers(headers);

        Response response = rq.get(path + "1bfff94a-b70e-4b39-bd2a-be1c0f898589");

        assertEquals(200, response.getStatusCode());
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM