Hey! I'm probably overlooking something simple her...
# pact-jvm
b
Hey! I'm probably overlooking something simple here, but when I run this test:
Copy code
package customer;

import static <http://org.hamcrest.CoreMatchers.is|org.hamcrest.CoreMatchers.is>;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.core.IsEqual.equalTo;

import au.com.dius.pact.consumer.MockServer;
import au.com.dius.pact.consumer.dsl.DslPart;
import au.com.dius.pact.consumer.dsl.LambdaDsl;
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.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;

import java.io.IOException;
import java.util.UUID;

@ExtendWith(PactConsumerTestExt.class)
@PactTestFor(providerName = "address_provider")
public class AddressServiceGetContractTest {

    private static final UUID ID = UUID.fromString("8aed8fad-d554-4af8-abf5-a65830b49a5f");
    private static final String ADDRESS_TYPE = "billing";
    private static final String STREET = "Main Street";
    private static final int NUMBER = 123;
    private static final String CITY = "Nothingville";
    private static final int ZIP_CODE = 54321;
    private static final String STATE = "Tennessee";
    private static final String COUNTRY = "United States";

    @Pact(provider = "address_provider", consumer = "customer_consumer")
    public RequestResponsePact pactForGetExistingAddressId(PactDslWithProvider builder) {

        DslPart body = LambdaDsl.newJsonBody((o) -> o
                .uuid("id", ID)
                .stringType("addressType", ADDRESS_TYPE)
                .stringType("street", STREET)
                .integerType("number", NUMBER)
                .stringType("city", CITY)
                .integerType("zipCode", ZIP_CODE)
                .stringType("state", STATE)
                .stringType("country", COUNTRY)
        ).build();

        return builder.given(
                "Customer GET: the address ID matches an existing address")
                .uponReceiving("A request for address data")
                .path(String.format("/address/%s", ID))
                .method("GET")
                .willRespondWith()
                .status(200)
                .body(body)
                .toPact();
    }

    @Test
    public void testFor_GET_existingAddressId_shouldYieldExpectedAddressData(MockServer mockServer) throws IOException {

        HttpResponse httpResponse = Request.Get(mockServer.getUrl() + "/address/" + ID).execute().returnResponse();
        assertThat(httpResponse.getStatusLine().getStatusCode(), is(equalTo(200)));
    }
}
I get the following error message:
Copy code
[ERROR] Failures: 
[ERROR]   AddressServiceGetContractTest The following methods annotated with @Pact were not executed during the test: AddressServiceGetContractTest.pactForGetExistingAddressI
d
If these are currently a work in progress, add a @Disabled annotation to the method

[ERROR] Errors: 
[ERROR]   AddressServiceGetContractTest.testFor_GET_existingAddressId_shouldYieldExpectedAddressData(MockServer) ยป UnsupportedOperation Method pactForGetExistingAddressId doe
s not conform required method signature 'public au.com.dius.pact.core.model.V4Pact xxx(PactBuilder builder)'
Any ideas? I'm upgrading my Pact Java workshop material to finally use the latest Pact versions and JUnit 5, and until this point the docs helped out well, but I'm stuck now.
r
@Bas Dijkstra Add the annotation to your test method
@PactTestFor(pactMethod = "pactForGetExistingAddressId")
. You should also keep the
@Test
annotation. So your test method should be like this
Copy code
@Test
@PactTestFor(pactMethod = "pactForGetExistingAddressId")
public void testFor_GET_existingAddressId_shouldYieldExpectedAddressData(MockServer mockServer) throws IOException {

        HttpResponse httpResponse = Request.Get(mockServer.getUrl() + "/address/" + ID).execute().returnResponse();
        assertThat(httpResponse.getStatusLine().getStatusCode(), is(equalTo(200)));
    }
b
Awesome, Iโ€™ll try that right away tomorrow morning, thank you @Riley Marzka !
youre welcome 1
I've added that annotation but unfortunately, the error message remains. Does anybody have any other ideas?
Fixed it! The solution was to explicitly specify that the Pact version should be V3, i.e. the class-level annotation should be `
Copy code
@PactTestFor(providerName = "address_provider", pactVersion = PactSpecVersion.V3)
What was causing the confusion was this docs page: https://docs.pact.io/implementation_guides/jvm/consumer/junit5, where it gives the example I followed but that argument to the annotation isn't there. Seems like this method signature
public RequestResponsePact pactForGetExistingAddressId(PactDslWithProvider builder)
is compatible with Pact specification V3 only, but not with V4. Good to know!
@Riley Marzka thank you for your suggestion anyway, I noticed that I do also need to add that annotation at the test level once I have more than one
@Pact
method and associated test in my test class, otherwise Pact has no idea how to match the two.
m
๐Ÿ™Œ
r
Glad you got it figured out and glad I could help (even if tangentially)!
๐Ÿ™Œ 1
๐Ÿ™ 1