Testing Microservices With Consumer-Driven Contracts
Testing Microservices With Consumer-Driven Contracts
Consumer-driven contracts
Andrew Morgan
@mogronalol
Microservices testing is hard
Consumer driven contract testing
About Me
• Independent Consultant
specialising in microservices
& continuous delivery
• PluralSight Author
• InfoQ Editor
Andrew Morgan
@mogronalol
Agenda
Rewards
Accounts
Payments
Loans
What are Microservices?
HTTP Rewards
Accounts
HTTP
Payments
Loans
What are Microservices?
HTTP Rewards
Accounts
Messaging
HTTP
Payments
Loans
The root cause of testing challenges
REST API
Payments Booking
End-to-end Testing Microservices
Build time
Debuggability
Infrastructure cost
Operations
Flakiness
Distributed monolith
Mocking or Stubbing Dependencies
False positives
Staleness
Difficult to maintain
The Weak Feedback Loop
Run individual
microservice Build and deploy
tests to stage End-to-end test Release
???
Consumer Provider
Key Contract Characteristics
Continuously tested
No specific protocol
Scenario:
Scenario:
Required Request:
GET /flights?from=London&to=Paris
What is an Interaction?
Scenario:
Required Request:
GET /flights?from=London&to=Paris
Given Response:
200 []
The Consumer Side
Generates
Generates
Pre-defined
contract
The Benefits
No stale stubs
Calculates credit
score
Credit Card Application Feature
Write a
failing test
A Failing Test
A Failing Test
A Failing Test
Implementing Our Endpoint
JUnit Test
Implementing Our Endpoint
Fails here
Following the CDC Workflow
Write a Define
failing test Contract
Contract Definition Language
Request made
by our test
Response expected
by our test
Following the CDC Workflow
Boots WireMock
Fails here
…to a Passing Test
Passes here
Following the CDC Workflow
Implement me
“Consumer-driven contracts are like TDD
applied at the API level.”
- Marcin Grzejszczak
Agenda
JSON XPath
XML
Ignore casing
Consumer Provider
Message-driven CDC
Pact
Questions?
https://www.pluralsight.com/profile/author/andrew-morgan
@mogronalol