contract-testing-demo
pact and java contract testing with micro-commits
commit e24373ae5f07234d4c9406771dbdf1a2868056e2 parent 7e4e8a53cc000cf15162762ef4dee1ef9d5db25f Author: Jul <jul@9o.is> Date: Wed, 26 Jun 2019 11:47:03 -0400 Write failing consumer test Test: Given there exists an alligator named Mary, a request for Mary the alligator returns Mary the alligator. Diffstat:
| M | consumer-driven/zoo-service/src/test/java/AnimalServicePactTest.java | | | 45 | ++++++++++++++++++++++++++++++++++++++++++++- |
1 file changed, 44 insertions(+), 1 deletion(-)
diff --git a/consumer-driven/zoo-service/src/test/java/AnimalServicePactTest.java b/consumer-driven/zoo-service/src/test/java/AnimalServicePactTest.java @@ -1,8 +1,51 @@ +import au.com.dius.pact.consumer.ConsumerPactBuilder; +import au.com.dius.pact.consumer.PactVerificationResult; +import au.com.dius.pact.model.MockProviderConfig; +import au.com.dius.pact.model.RequestResponsePact; import org.junit.Test; + +import java.util.HashMap; +import java.util.Map; + +import static au.com.dius.pact.consumer.ConsumerPactRunnerKt.runConsumerTest; import static org.junit.Assert.*; public class AnimalServicePactTest { - public void alligatorMaryExists() {} + + @Test + public void alligatorMaryExists() { + Map<String, String> headers = new HashMap<>(); + headers.put("Content-type", "application/json"); + + RequestResponsePact pact = ConsumerPactBuilder + .consumer("Zoo Service") + .hasPactWith("Animal Service") + .given("there exists an alligator named Mary") + .uponReceiving("a request for Mary the alligator") + .method("GET") + .path("/alligators/Mary") + .willRespondWith() + .status(200) + .headers(headers) + .body("{\"name\": \"Mary\"}") + .toPact(); + + MockProviderConfig config = MockProviderConfig.createDefault(); + + PactVerificationResult result = runConsumerTest(pact, config, (mockServer, context) -> { + AnimalService animalService = new AnimalService(mockServer.getUrl()); + Alligator alligator = animalService.getAlligator("Mary"); + + assertEquals("Mary", alligator.getName()); + }); + + if (result instanceof PactVerificationResult.Error) { + throw new RuntimeException(((PactVerificationResult.Error)result).getError()); + } + + assertEquals(PactVerificationResult.Ok.INSTANCE, result); + } + public void alligatorMaryDoesNotExist() {} public void animalServiceIsUnavailable() {} }