简体   繁体   中英

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. We use JUnit4 for others, but this one will be JUnit5. The error occurs when running the JUnit5 test using the pact annotation on the RequestResponsePact method.

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

I've seen Basic Pact/Junit5 Test Setup fails. 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. 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" ).

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. 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());
    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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