Developing event-driven microservices with event sourcing and ...
Testing microservices with consumer-driven …...•Introducing microservices testing challenges •...
Transcript of Testing microservices with consumer-driven …...•Introducing microservices testing challenges •...
Testing Java Microservices with Consumer-driven contracts
Andrew Morgan @mogronalol
Microservices testing is hard
Consumer driven contract testing
Andrew Morgan @mogronalol
• Independent Consultant specialising in microservices & continuous delivery
• PluralSight Author • InfoQ Editor
About Me
• Introducing microservices testing challenges
• Consumer-driven contract testing overview
• Following through the workflow with Spring Cloud Contract
• Advanced consumer-driven contract concepts
Agenda
• Introducing microservices testing challenges
• Consumer-driven contract testing overview
• Following through the workflow with Spring Cloud Contract
• Advanced consumer-driven contract concepts
Agenda
Payments
Accounts
Loans
What are Microservices?
Rewards
HTTP
HTTP
Payments
Accounts
Loans
What are Microservices?
Rewards
MessagingHTTP
HTTP
Payments
Accounts
Loans
What are Microservices?
Rewards
The root cause of testing challenges
Payments
REST API
Booking
End-to-end Testing Microservices
Automated tests Browser or other UI Environment
Build time
Debuggability
Infrastructure cost
Operations
Flakiness
Distributed monolith
Disadvantages of End-to-end Testing
Automated tests Microservice Mock or Stub of Dependency
Mocking or Stubbing Dependencies
False positives
Staleness
Difficult to maintain
Disadvantages of Mocking or Stubbing
The Weak Feedback Loop
Run individual microservice
testsBuild and deploy
to stage End-to-end test
False positives
Release
Slow and flaky
• Introducing microservices testing challenges
• Consumer-driven contract testing overview
• Following through the workflow with Spring Cloud Contract
• Advanced consumer-driven contract concepts
Agenda
What is Consumer-driven Contract Testing?
Fills the microservices testing gap
Continuous testing against contracts
Consumers drive the implementation of providers
Independently testable and releasable microservices
???
Test Architecture Overview
Consumer Provider
Key Contract Characteristics
An agreed interaction between microservices
Continuously tested
No specific protocol
Not the same stubbing!
Definitely not API documentation!
What is an Interaction?
Scenario:
Should return no flights between London and Paris
What is an Interaction?
Scenario:
Should return no flights between London and Paris
Required Request:
GET /flights?from=London&to=Paris
What is an Interaction?
Scenario:
Should return no flights between London and Paris
Required Request:
GET /flights?from=London&to=Paris
Given Response:
200 []
The Consumer Side
Automated tests
Consuming Microservice
Stub of provider microservice
Generates
The interaction our test depends on
The Provider Side
Pre-defined contract
Providing Microservice
Contract verification tests
Generates
Fast and reliable feedback
Less money spent on infrastructure
API delivers exactly what is required
Understanding dependency hell
No stale stubs
Easy to work in parallel
The Benefits
• Introducing microservices testing challenges
• Consumer-driven contract testing overview
• Following through the workflow with Spring Cloud Contract
• Advanced consumer-driven contract concepts
Agenda
Spring Cloud Contract Overview
JVM-based CDC framework
Spring Boot Integration
Groovy Based Contract Definition Language
Message-driven and HTTP-driven interactions
Credit Card Application Feature
Credit Card Application Feature
JUnit Test Credit card applications service
Credit score service
Credit Card Application Feature
JUnit Test Credit card applications service
Credit score service
Applies for credit card
Credit Card Application Feature
JUnit Test Credit card applications service
Credit score service
Calculates credit score
Credit Card Application Feature
JUnit Test Credit card applications service
Credit score service
Accepts or rejects based on credit score
Following the CDC Workflow
Write a failing test
Define Contract
Build stubs
Make tests pass
Build verification
tests
Make tests pass
Following the CDC Workflow
Write a failing test
A Failing Test
A Failing Test
A Failing Test
Implementing Our Endpoint
JUnit Test
JUnit Test Credit card applications service
Implementing Our Endpoint
JUnit Test Credit card applications service
Credit score service
Does not exist
Implementing Our Endpoint
JUnit Test Credit card applications service
Credit score service
Implementing Our Endpoint
Fails here
Following the CDC Workflow
Write a failing test
Define Contract
Contract Definition Language
Request made by our test
Response expected by our test
Following the CDC Workflow
Write a failing test
Define Contract
Build stubs
Stub Generation
Contract Spring Cloud Contract Plugin
Stub Jar
wiremock.org
Following the CDC Workflow
Write a failing test
Define Contract
Build stubs
Make tests pass
The Stub Runner
Download given stubs
Boots WireMock
Imports stubs into Wiremock
From a Failing Test
Unit test Credit card applications service
Credit score service
Fails here
…to a Passing Test
Unit test Credit card applications service
Spring Cloud Contract Stub
Passes here
Following the CDC Workflow
Write a failing test
Define Contract
Build stubs
Make tests pass
Build verification
tests
Provider Test Generation
Contract Spring Cloud Contract Plugin
Verification Tests
Following the CDC Workflow
Write a failing test
Define Contract
Build stubs
Make tests pass
Build verification
tests
Make tests pass
From Failing…
Contract Verification Tests
Credit score service
To Passing…
Contract Verification Tests
Credit score service
Implement me
- Marcin Grzejszczak
“Consumer-driven contracts are like TDD applied at the API level.”
• Introducing microservices testing challenges
• Consumer-driven contract testing overview
• Following through the workflow with Spring Cloud Contract
• Advanced consumer-driven contract concepts
Agenda
Does this only work with exact comparisons?
Regex
JSON
XML
XPath
Subsets
Starts with
Ignore casing
More
What about messaging?
Message-driven CDC
Consumer Provider
Message-driven CDC
Consumer ProviderBroker
Why Messaging?
Message schemas and queues are shared
Asynchronous failures are a debugging nightmare!
Brokers are slow
Where do we store our contracts?
Contract Repository
Contract Repository
Talk Summary
Don’t build and test microservices like a monolith
Consumer-driven contracts promote independence
Leverage frameworks like Spring Cloud Contract and
Pact
https://www.pluralsight.com/profile/author/andrew-morgan
Questions?
@mogronalol