contract-testing-demo

pact and java contract testing with micro-commits

git clone https://9o.is/git/contract-testing-demo.git

commit c2668b9ac766f7dfe7b6f1e23b306cfcdbbe41b5
parent b7de40d73f97cb4c9b4fb3ac31e1435a06f2e0c1
Author: Jul <jul@9o.is>
Date:   Wed, 26 Jun 2019 15:13:44 -0400

Write failing consumer test

Test:
Given there does not exist an alligator named Mary, upon receiving a
request for Mary the alligator, a 404 is returned.

Diffstat:
Mconsumer-driven/zoo-service/src/test/java/AnimalServicePactTest.java | 30+++++++++++++++++++++++++++++-
1 file changed, 29 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 @@ -46,6 +46,34 @@ public class AnimalServicePactTest { assertEquals(PactVerificationResult.Ok.INSTANCE, result); } - public void alligatorMaryDoesNotExist() {} + @Test + public void alligatorMaryDoesNotExist() { + RequestResponsePact pact = ConsumerPactBuilder + .consumer("Zoo Service") + .hasPactWith("Animal Service") + .given("there does not exist an alligator named Mary") + .uponReceiving("a request for Mary the alligator") + .method("GET") + .path("/alligators/Mary") + .willRespondWith() + .status(404) + .toPact(); + + MockProviderConfig config = MockProviderConfig.createDefault(); + + PactVerificationResult result = runConsumerTest(pact, config, (mockServer, context) -> { + AnimalService animalService = new AnimalService(mockServer.getUrl()); + Alligator alligator = animalService.getAlligator("Mary"); + + assertNull(alligator); + }); + + if (result instanceof PactVerificationResult.Error) { + throw new RuntimeException(((PactVerificationResult.Error)result).getError()); + } + + assertEquals(PactVerificationResult.Ok.INSTANCE, result); + } + public void animalServiceIsUnavailable() {} }