I started out by reading:
https://docs.pact.io/faq/convinceme.html
Then watching:
Those 2 resources convinced me to give it a shot.
So I set out and created a quick set of Spring boot apps, the GitHub repo here, to test out the concepts and get everything working.
To highlight some important bits from the demo.
Consumer:
As Pact is a consumer driven test framework. This is where you define a unit test, that test mocks the http server response and you assert against that.
Once the test is successful it creates a pact json file in the /pacts directory.
So after the "mock" test is run and the pact file has been created. You need to include a maven plugin ...pact... that is then used to publish the content of the pacts/ folder to the pact broker... which is defined in the pom as below.
Producer:
This uses the JUnit integration from Pact.io to download the pacts from the broker and then run against an running service.
Since this already uses a @RunWith annotation, I could not use the spring boot runner. So to get around that as a before class step, I start the Spring boot application, the pacts then gets run against that running instance... and the boot application gets stopped again after the tests. Depending on your use case I guess it would also be an option to do this with @Before so you get a new service instance started before each pack, but that would slow down the execution tremendously.
The @State annotation, allows for clients to define a specific state, which the producer can the use to setup additional data / conditions required for the test to run.
Once the pacts have executed against the service there are reports generated in the target folder.
Setting up the Pact Broker
1. Grab the public images from Docker Hub.
docker pull dius/pact_broker
docker pull postgres
2. Then setup the Postgres DB
docker run --name pactbroker-db -e POSTGRES_PASSWORD=ThePostgresPassword -e POSTGRES_USER=admin -d postgres3. Once the DB is up, run the actual Broker:
docker run -it --link pactbroker-db:postgres --rm postgres psql -h postgres -U admin
CREATE USER pactbrokeruser WITH PASSWORD 'TheUserPassword';
CREATE DATABASE pactbroker WITH OWNER pactbrokeruser;
GRANT ALL PRIVILEGES ON DATABASE pactbroker TO pactbrokeruser;
docker run --name pactbroker --link pactbroker-db:postgres -e PACT_BROKER_DATABASE_USERNAME=pactbrokeruser -e PACT_BROKER_DATABASE_PASSWORD=TheUserPassword -e PACT_BROKER_DATABASE_HOST=postgres -e PACT_BROKER_DATABASE_NAME=pactbroker -d -p 80:80 dius/pact_broker
Extra References:
https://docs.pact.io/documentation/
https://docs.pact.io/documentation/sharings_pacts.html
https://github.com/DiUS/pact-jvm
https://github.com/DiUS/pact-jvm/tree/master/pact-jvm-consumer-junit
Get the example project
https://github.com/bdupreez/pactdemo