Consumer Driven Contract Testing
Using Pact
Introduction
Gopinath Langote
Application Developer, ThoughtWorks
Problem
Payment Service Customer Service
Request
Response
Problem
Payment Service
Request
Response
{
“customerName” : ”John”
}
{
“customerName” : ”John”
}
Customer Service
Refactor !!!
Payment Service
Request
Response
{
“name” : ”John”
}
{
“customerName” : ”John”
}
Customer Service
Test Pyramid
End To End
Test
Integration
Test
Unit Test
Unit Test
Unit Test
➢Does not know about external service
➢Limited to the method level
Integration Test
Integration Test
Request
Response
Payment Service
Mocked Customer Service Client
➢Not Trustworthy
➢Entire application should be up
Integration Test
End to end Test
End to End Test
Request
Response
Payment Service User Service
➢Slow
➢All services need to be up before
running the test
➢Trustworthy
➢Solves our problem. Finally !!!
End to End Test
Functional Test
Manual Test
Contract Test
Test Pyramid
End To End
Test
Integration
Test
Contract Test
Unit Test
Contract Test
Payment Service User Service
Request
Response
Consumer Provider
Contract Test
Register expectation
Request
Payment Service
Customer Service
PACT
TEST
Response
Contract Test
Payment Service
User Service
Get the contract
Assert the expectations from
consumer
Consumer
Provider
PACT
PACT
File
File System URL / Cloud
storage
Pact Broker
Initialize Mock Server
Specify the request - response
Test the response
Interactions to the server captured by pact
Providing State To the Provider
Pact Broker
Why should I use Pact
• Faster execution.
• Framework independent
• Reliable responses, not flakey tests.
• Causes of failure are easier to identify as only one component is being
tested at a time.
• No separate integration environment(s) required to be managed for
automated integration tests - pact tests run in standalone CI builds.
NOT YET CONVINCED ? https://docs.pact.io/faq/convinceme.html
Alternative to pacts
• Spring cloud contract https://cloud.spring.io/spring-cloud-contract/
• Swagger Validator http://bigstickcarpet.com/swagger-
parser/www/index.html
Read More
• Pact https://docs.pact.io/
• Pact JVM https://github.com/DiUS/pact-jvm
• Pact JVM implementations: https://github.com/gopinath-
langote/Pact-JVM-Implementation
• Pact All language support :
https://docs.pact.io/documentation/implementation_guides.html
• https://martinfowler.com/articles/consumerDrivenContracts.html
• Join the community at https://gitter.im/realestate-com-au/pact?utm_source=share-
link&utm_medium=link&utm_campaign=share-link
THANK YOU
For questions or suggestions:
Gopinath Langote
gopinathlangote11@gmail.com
33

vodQA(Pune) 2018 - Consumer driven contract testing using pact