From 80d8a877976cebb021e22bcb5fac3acb20b25e9a Mon Sep 17 00:00:00 2001 From: Ronald Holshausen Date: Fri, 6 Dec 2024 09:38:52 +1100 Subject: [PATCH] chore: Add example of a test with a pending interaction --- .../consumer/junit5/V4PendingPactTest.java | 51 +++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 consumer/junit5/src/test/java/au/com/dius/pact/consumer/junit5/V4PendingPactTest.java diff --git a/consumer/junit5/src/test/java/au/com/dius/pact/consumer/junit5/V4PendingPactTest.java b/consumer/junit5/src/test/java/au/com/dius/pact/consumer/junit5/V4PendingPactTest.java new file mode 100644 index 000000000..baa892a3f --- /dev/null +++ b/consumer/junit5/src/test/java/au/com/dius/pact/consumer/junit5/V4PendingPactTest.java @@ -0,0 +1,51 @@ +package au.com.dius.pact.consumer.junit5; + +import au.com.dius.pact.consumer.MockServer; +import au.com.dius.pact.consumer.dsl.PactBuilder; +import au.com.dius.pact.core.model.V4Pact; +import au.com.dius.pact.core.model.annotations.Pact; +import org.apache.hc.client5.http.fluent.Request; +import org.apache.hc.core5.http.ClassicHttpResponse; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; + +import java.io.IOException; + +import static au.com.dius.pact.consumer.dsl.Matchers.notEmpty; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.Matchers.is; + +@ExtendWith(PactConsumerTestExt.class) +@PactTestFor(providerName = "v4_pending_provider") +public class V4PendingPactTest { + + @Pact(consumer="v4_test_consumer") + public V4Pact httpInteraction(PactBuilder builder) { + return builder + .expectsToReceiveHttpInteraction("Pending interaction", httpBuilder -> { + return httpBuilder + .withRequest(requestBuilder -> requestBuilder + .path("/") + .method("GET")) + .willRespondWith(responseBuilder -> responseBuilder + .status(200) + .body("{\"responsetest\": true, \"version\": \"v3\"}") + .header("test", notEmpty("Example")) + ) + .comment("Marking this as pending") + .pending(true); + }) + .toPact(); + } + + @Test + @PactTestFor(pactMethod = "httpInteraction") + void runHttpTest(MockServer mockServer) throws IOException { + ClassicHttpResponse httpResponse = (ClassicHttpResponse) Request.get(mockServer.getUrl()).execute().returnResponse(); + assertThat(httpResponse.getCode(), is(200)); + assertThat(new String(httpResponse.getEntity().getContent().readAllBytes()), + is(equalTo("{\"responsetest\": true, \"version\": \"v3\"}"))); + assertThat(httpResponse.containsHeader("test"), is(true)); + } +}