Bas Dijkstra
08/25/2023, 2:02 PMpackage 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:
[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.Riley Marzka
08/25/2023, 8:38 PM@PactTestFor(pactMethod = "pactForGetExistingAddressId") . You should also keep the @Test annotation.
So your test method should be like this
@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)));
}Bas Dijkstra
08/25/2023, 8:48 PMBas Dijkstra
08/26/2023, 6:25 AMBas Dijkstra
08/26/2023, 6:36 AM@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!Bas Dijkstra
08/26/2023, 6:49 AM@Pact method and associated test in my test class, otherwise Pact has no idea how to match the two.Matt (pactflow.io / pact-js / pact-go)
Riley Marzka
08/28/2023, 3:14 PM