Sample Pact Contract Testing

Details can be found here pact contract testing. Comparison with other tools can be found here The same project is implemented in spring cloud contract testing here. Refer to it for comparison

Pact is a consumer driven contract testing

consumer

  • A consumer uses the pact framework to define the expected behaviour from a producer. There are multiple ways to do it, details can be found here
  • Running the test will store the interaction in a json file which can be found in build/pacts (this path can be changed)
  • Push the interaction json file into a pact broker (this step is optional as the file can be shared multiple ways)
    • docker compose up at the root of this project. This starts a pact broker
    • ./gradlew consumer:pactPublish
    • The published contract can be viewed in this url
  • Now it is the job of the producer to confirm to expected contract from the client/consumer

Producer

  • Pact provider a mechanism where test are autogenerated to test if the producer confirm to the contract. This is what is used in this project(https://docs.pact.io/implementation_guides/jvm/provider/junit5spring)
  • There are multiple ways that a producer can test against the expected contract. Refer to docs for more details
  • Run the test via ./gradlew produer:test
  • Assuming all the tests passes then it can be said that the producer has conformed to expected contract from the consumer