Simple example project that uses pacts with Java.
There are 2 sub projects:
- provider - A rest service with a hello world endpoint.
- consumer - A consumer of the hello world endpoint
With this project you will be able to
- Create a pact using the consumer
- Publish it to a pact broker
- Verify that the provider fulfills the pact
In the pact-broker folder there is a docker compose file. This manages starting up a pact broker and a postgres DB that it requires
cd pact-broker
docker-compose up
Try navigating to localhost:80 in a browser and you should see the pact broker UI but no pacts... yet.
Consumers are always responsible for creating pacts.
A json file describing the pact can be generated with out example consumer by simply running the test. This occurs because the tests are annotated with annotations provided by the pact JVM plugin.
The test code is actually starting a mock endpoint for the test and recording the interactions.
To create a pact in the consumer/build/pacts simply run:
./gradlew :consumer:test
You can publish the pact using (Note that the test intermediate step is not necessary as pactPublish depends on test):
./gradlew :consumer:pactPublish
If all goes well you should now see a pact present in the pact broker UI.
Ideally this would be handled as part of the verify task itself and I'll probably make this happen at some point. However for now you will need to start up the service. Do this in a new shell tab:
./gradlew :provider:build
java -jar provider/build/libs/gs-actuator-service-0.1.0.jar
Now you can verify the pact against the service you have just started
Just do this:
./gradlew :provider:pactVerify
You should see some output saying the pact was verified.
I explicitly tag the pact with INTEGRATION. This is just experimenting.
When fetching pacts to verify it gets the latest version with this tag. In the current setup it also verifies the latest version regardless of tag.
Don't seem to be able to verify against versions.
In the real world these would be in separate repositories and pacts would be published and verified on the CI server.
Thanks to 'The Creative Tester' as this is heavily based on their work: