Testing Java Microservices with Consumer-driven contracts
Andrew Morgan @mogronalol
Testing Java Microservices with Consumer-driven contracts Andrew - - PowerPoint PPT Presentation
Testing Java Microservices with Consumer-driven contracts Andrew Morgan @mogronalol Microservices testing is hard Consumer driven contract testing About Me Independent Consultant specialising in microservices & continuous delivery
Andrew Morgan @mogronalol
Andrew Morgan @mogronalol
specialising in microservices & continuous delivery
challenges
with Spring Cloud Contract
contract concepts
challenges
with Spring Cloud Contract
contract concepts
Payments Accounts Loans
Rewards
HTTP HTTP
Payments Accounts Loans
Rewards
Messaging HTTP HTTP
Payments Accounts Loans
Rewards
Payments
REST API
Booking
Automated tests Browser or other UI Environment
Automated tests Microservice Mock or Stub of Dependency
Run individual microservice tests Build and deploy to stage End-to-end test False positives Release Slow and flaky
challenges
with Spring Cloud Contract
contract concepts
Fills the microservices testing gap Continuous testing against contracts Consumers drive the implementation of providers Independently testable and releasable microservices
Consumer Provider
An agreed interaction between microservices Continuously tested No specific protocol Not the same stubbing! Definitely not API documentation!
Scenario: Should return no flights between London and Paris
Scenario: Should return no flights between London and Paris Required Request: GET /flights?from=London&to=Paris
Scenario: Should return no flights between London and Paris Required Request: GET /flights?from=London&to=Paris Given Response: 200 []
Automated tests Consuming Microservice Stub of provider microservice Generates The interaction our test depends on
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
challenges
with Spring Cloud Contract
contract concepts
JVM-based CDC framework Spring Boot Integration Groovy Based Contract Definition Language Message-driven and HTTP-driven interactions
JUnit Test Credit card applications service Credit score service
JUnit Test Credit card applications service Credit score service
Applies for credit card
JUnit Test Credit card applications service Credit score service
Calculates credit score
JUnit Test Credit card applications service Credit score service
Accepts or rejects based
Write a failing test Define Contract Build stubs Make tests pass Build verification tests Make tests pass
Write a failing test
JUnit Test
JUnit Test Credit card applications service
JUnit Test Credit card applications service Credit score service
Does not exist
JUnit Test Credit card applications service Credit score service
Fails here
Write a failing test Define Contract
Request made by our test Response expected by our test
Write a failing test Define Contract Build stubs
Contract Spring Cloud Contract Plugin Stub Jar
wiremock.org
Write a failing test Define Contract Build stubs Make tests pass
Download given stubs Boots WireMock Imports stubs into Wiremock
Unit test Credit card applications service Credit score service Fails here
Unit test Credit card applications service Spring Cloud Contract Stub Passes here
Write a failing test Define Contract Build stubs Make tests pass Build verification tests
Contract Spring Cloud Contract Plugin Verification Tests
Write a failing test Define Contract Build stubs Make tests pass Build verification tests Make tests pass
Contract Verification Tests Credit score service
Contract Verification Tests Credit score service Implement me
challenges
with Spring Cloud Contract
contract concepts
Consumer Provider
Consumer Provider Broker
Message schemas and queues are shared Asynchronous failures are a debugging nightmare! Brokers are slow
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
@mogronalol