This project contains a very simple demo of using consumer-driven contracts to verify the interactions between microservices. It leverages Spring Boot for both the provider and consumer services.
-
Run the tests using Gradle:
$ ./gradlew test
-
This will result in the creation of a pact file called
Foo_Consumer-Foo_Provider.json
atmicroservices-pact/pacts
. Here’s a current example:{ "provider" : { "name" : "Foo_Provider" }, "consumer" : { "name" : "Foo_Consumer" }, "interactions" : [ { "description" : "a request for Foos", "request" : { "method" : "GET", "path" : "/foos" }, "response" : { "status" : 200, "headers" : { "Content-Type" : "application/json;charset=UTF-8" }, "body" : [ { "value" : 42 }, { "value" : 100 } ] } } ], "metadata" : { "pact-specification" : { "version" : "2.0.0" }, "pact-jvm" : { "version" : "2.1.7" } } }
By virtue of the fact that these tests pass, we know that the
microservices-pact-consumer
app interacts appropriately with the contract as defined in ConsumerPortTest.java. -
Then, in another terminal window, run the pact verification:
$ ./gradlew pactVerify
You should see output along these lines:
$ ./gradlew pactVerify :microservices-pact-provider:pactVerify_fooProvider Verifying a pact between fooConsumer and fooProvider [Using file /Users/pivotal/workspace/microservices-pact/pacts/Foo_Consumer-Foo_Provider.json] a request for Foos returns a response which has status code 200 (OK) includes headers "Content-Type" with value "application/json;charset=UTF-8" (OK) has a matching body (OK) :microservices-pact-provider:pactVerify BUILD SUCCESSFUL