This is a Maven-based port of the Gradle-based example (https://github.com/mstine/microservices-pact by Matt Stine <mstine@pivotal.io>).
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 build with tests using Maven:
$ mvn clean install
-
This will result in the creation of a pact file called
Foo_Consumer-Foo_Provider.json
atmicroservices-pact-maven/microservices-pact-consumer/target/pacts
. Here’s a current example:{ "provider": { "name": "Foo_Provider" }, "consumer": { "name": "Foo_Consumer" }, "interactions": [ { "providerState": null, "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": "3.2.2" } } }
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. -
Next, we want to verify that the
microservices-pact-provider
app actually provides the expected contract. In one terminal window, start up the provider app:$ java -jar microservices-pact-provider/target/fat-microservices-pact-provider-1.0.0-SNAPSHOT.jar
-
Then, in another terminal window, run the pact verification:
$ mvn au.com.dius:pact-jvm-provider-maven_2.11:verify
You should see output along these lines:
[INFO] --- pact-jvm-provider-maven_2.11:2.1.9:verify (default-cli) @ microservices-pact-consumer --- Verifying a pact between Foo_Consumer and Foo_Provider [Using file /home/anga/projects/pact-testing/microservices-pact-maven/microservices-pact-consumer/target/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) [INFO] [INFO] ------------------------------------------------------------------------ [INFO] Building microservices-pact-provider 1.0.0-SNAPSHOT [INFO] ------------------------------------------------------------------------ [INFO] [INFO] --- pact-jvm-provider-maven_2.11:3.2.2:verify (default-cli) @ microservices-pact-provider --- [INFO] ------------------------------------------------------------------------ [INFO] Reactor Summary: [INFO] [INFO] microservices-pact-maven ........................... SUCCESS [ 0.518 s] [INFO] microservices-pact-consumer ........................ SUCCESS [ 1.134 s] [INFO] microservices-pact-provider ........................ SUCCESS [ 0.003 s] [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 1.753 s [INFO] Finished at: 2016-01-18T13:47:47+02:00 [INFO] Final Memory: 23M/487M [INFO] ------------------------------------------------------------------------