SlideShare a Scribd company logo
Consumer Driven Contract Testing
Mike van Vendeloo @mikevanvendeloo
@mikevanvendeloo
About me
Mike van Vendeloo
Software Craftsman
Scrum Master
Focussed on software quality and automation
@mikevanvendeloo
Agenda
1. Background
2. Consumer Driven Contract Testing
3. Spring Cloud Contract
@mikevanvendeloo
@mikevanvendeloo
@mikevanvendeloo
@mikevanvendeloo
@mikevanvendeloo
@mikevanvendeloo
Consumer Driven Contracts to the rescue
Consumer Driven Contracts is a pattern that drives the
development of the Producer from its Consumers point of view.
@mikevanvendeloo
Terminology
A Contract is a collection of agreements between a client
(Consumer) and an API (Producer) that describes the
interactions that can take place between them.
Consumer: Service listening to messages or requesting API methods
Producer: Service sending messages or exposing an API
Contract: Agreement between producer and consumer how the API (method,
request and response) / message will look like
@mikevanvendeloo
Technology Radar
“The concept isn’t new, but with the mainstream
acceptance of microservices, we need to remind
people that consumer-driven contracts are an
essential part of a mature microservice testing
portfolio, enabling independent service
deployments.”
@mikevanvendeloo
Consumer Driven Contract Testing
“TDD on the architectural level”
“Use an API before implementing it”
-- Marcin Grzejszczak --
@mikevanvendeloo
Frameworks
Pact - https://docs.pact.io/
Pact-JVM - https://github.com/DiUS/pact-jvm
Karma-Pact - https://github.com/pact-foundation/karma-pact
Pacto - http://thoughtworks.github.io/pacto/
AccuRest - https://github.com/Codearte/accurest -> Spring Cloud Contract
And many more...
@mikevanvendeloo
Consumer
Spring Cloud Contract
Producer
A
P
I
Consumer
@mikevanvendeloo
Spring Cloud Contract
● Spring Cloud Contract Verifier
● Spring Cloud Contract Wiremock
● Spring Cloud Contract Stubrunner
@mikevanvendeloo
Contract DSL
package net.vanvendeloo.demos.comicbooks
org.springframework.cloud.contract.spec.Contract.make {
description(“”” Some explanation in given/when/then style “””)
request {
method 'PUT'
url '/comicbook'
body ([ title : ‘Game of drones’, isbn: ‘9789002259722’])
}
response {
status 201
}
}
@mikevanvendeloo
Contract
verifier tests
Producer side
Maven
repository
stubs.jar
contract
Producer
A
P
I
generate
generate
@mikevanvendeloo
You can easily compile Contracts to WireMock stubs mapping using standalone maven
command:
mvn org.springframework.cloud:spring-cloud-contract-maven-plugin:convert
Producer side - Wiremock
@mikevanvendeloo
{
"id" : "2656c2c0-64b1-4d54-8ccd-8b5db4304248",
"request" : {
"url" : "/comics",
"method" : "GET"
},
"response" : {
"status" : 200,
"body" : "[{"id":1,"name":"Suske en Wiske","comicBooks":[{"id":1,"title":"Game of
drones","isbn":"9789002259722","number":337}]}]",
"headers" : {
"Content-Type" : "application/json"
},
"transformers" : [ "response-template" ]
},
"uuid" : "2656c2c0-64b1-4d54-8ccd-8b5db4304248"
}
Producer side - Stubs
@mikevanvendeloo
<plugin>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-contract-maven-plugin</artifactId>
<version>${spring-cloud-contract.version}</version>
<extensions>true</extensions>
<configuration>
<packageWithBaseClasses>net.vanvendeloo.demo.comicbooks</packageWi
thBaseClasses>
</configuration>
</plugin>
Producer side - Generate tests and stubs
@mikevanvendeloo
package net.vanvendeloo.demo.cloudcontract.comiccollection;
import org.junit.Before;
import com.jayway.restassured.module.mockmvc.RestAssuredMockMvc;
public class ComicCollectionTestBase {
@Before
public void setup() {
RestAssuredMockMvc.standaloneSetup(new ComicCollectionController());
}
}
Producer side - Base class
@mikevanvendeloo
Consumer side
Maven
repository
stubs.jar
Consumer
Stub
runner
T
E
S
T
S
download
@mikevanvendeloo
Consumer side - Using stubs (1)
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-contract-stub-runner</artifactId>
<scope>test</scope>
</dependency>
@mikevanvendeloo
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment=WebEnvironment.NONE)
@AutoConfigureStubRunner(ids =
{"net.vanvendeloo.demo.comicbooks:comic-collections-api:+:stubs:8090"
}, workOffline = true)
@DirtiesContext
public class ComicCollectionApplicationTest {
…
}
Consumer side - Using stubs (2)
@mikevanvendeloo
Advanced contracts - Regular expressions
org.springframework.cloud.contract.spec.Contract.make {
request {
method 'POST'
url '/comicbook/1'
headers { contentType(applicationJson()) }
body(
title: $(consumer(regex(‘[A-Za-z ]+)), producer('My title')),
isbn: $(consumer(regex('[0-9]{10}')), producer('1234567890'))
)
}
...
@mikevanvendeloo
Set properties in the body
value(consumer(...), producer(...))
$(consumer(...), producer(...))
Reuse of request information in the response
fromRequest().body(String jsonPath)
Advanced contracts - Dynamic properties
@mikevanvendeloo
Test matchers
jsonPath('$.isbn', byRegex(number()))
jsonPath('$.returned', byRegex(anyBoolean()))
jsonPath('$.returndate, byRegex(byDate()))
Results in testcode
assertThat(parsedJson.read("$.isbn", String.class)).matches("-?d*(.d+)?");
assertThat(parsedJson.read("$.returned", String.class)).matches("(true|false)");
assertThat(parsedJson.read("$.returndate",
String.class)).matches("(dddd)-(0[1-9]|1[012])-(0[1-9]|[12][0-9]|3[01])");
Advanced contracts - Test matchers
@mikevanvendeloo
Ability to specify how the dynamic values should be generated by wiremock
jsonPath('$.isbn, byRegex(number()))
jsonPath('$.returned, byRegex(anyBoolean()))
jsonPath('$.returnDate, byDate())
{ "matchesJsonPath" : "$[?(@.isbn =~ /(-?d*(.d+)?)/)]"},
{ "matchesJsonPath" : "$[?(@.returned =~ /((true|false))/)]"},
{ "matchesJsonPath" : "$[?(@.returndate =~
/((dddd)-(0[1-9]|1[012])-(0[1-9]|[12][0-9]|3[01]))/)]"}
Advanced contracts - Stub matchers
@mikevanvendeloo
this.mockMvc.perform(post("/comicbooksearch")
.accept(MediaType.APPLICATION_JSON)
.contentType(MediaType.APPLICATION_JSON)
.content("{"number": 337 }"))
.andExpect(status().isOk())
.contentType(MediaType.valueOf("application/json"))
.stub("shouldReturnComicBook"))
// then Contract DSL documentation
.andDo(document("getComicBook",
SpringCloudContractRestDocs.dslContract()));
Advanced contracts - Generate from RestDocs
@mikevanvendeloo
Contract Repository
Sharing the contracts via a separate repository
<plugin>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-contract-maven-plugin</artifactId>
<configuration>
<contractsRepositoryUrl>http://link/to/your/nexus</contractsRepositoryUrl>
<contractDependency>
<groupId>net.vanvendeloo.demos.comiccollections</groupId>
<artifactId>contracts</artifactId>
</contractDependency>
</configuration>
</plugin>
@mikevanvendeloo
Contract Testing with messaging
Out of the box integration for using stubs with the following frameworks
● Spring Integration
● Spring Cloud Stream
● Apache Camel
● Spring AMQP
@mikevanvendeloo
Contract.make {
label 'return_comicbook_1'
input {
triggeredBy('bookReturnedTriggered()')
}
outputMessage {
sentTo('returnBook')
body('''{ "title" : "Game of drones" }''')
headers {
header('TITLE', 'Game of drones')
}
}
}
Contract for messages
@mikevanvendeloo
@mikevanvendeloo
Conclusions
● Communication is still the most important thing!
● CDC frameworks are still in development
● Spring Cloud Contract
○ integrates well with the Spring ecosystem
○ ensures reusable stubs
○ in the future, will provide improved tooling for non-Java
users
@mikevanvendeloo
@mikevanvendeloo
@mikevanvendeloo
References
https://martinfowler.com/articles/consumerDrivenContracts.html
https://github.com/spring-cloud-samples/spring-cloud-contract-samples
https://www.youtube.com/watch?v=sAAklvxmPmk&t=547s

More Related Content

PDF
Consumer-Driven Contract Testing
Paulo Clavijo
 
PPTX
vodQA(Pune) 2018 - Consumer driven contract testing using pact
vodQA
 
PPTX
Consumer-driven contracts: avoid microservices integration hell! (LondonCD - ...
Pierre Vincent
 
PDF
Consumer contract testing
Rafiq Gemmail
 
PDF
Contract testing and Pact
Seb Rose
 
PDF
Cloud-Native Streaming and Event-Driven Microservices
VMware Tanzu
 
PPTX
Contract testing. Isolated testing of microservices with pact.io - Evgeniy Ku...
Evgeniy Kuzmin
 
PDF
You've Made Kubernetes Available to Your Developers, Now What?
cornelia davis
 
Consumer-Driven Contract Testing
Paulo Clavijo
 
vodQA(Pune) 2018 - Consumer driven contract testing using pact
vodQA
 
Consumer-driven contracts: avoid microservices integration hell! (LondonCD - ...
Pierre Vincent
 
Consumer contract testing
Rafiq Gemmail
 
Contract testing and Pact
Seb Rose
 
Cloud-Native Streaming and Event-Driven Microservices
VMware Tanzu
 
Contract testing. Isolated testing of microservices with pact.io - Evgeniy Ku...
Evgeniy Kuzmin
 
You've Made Kubernetes Available to Your Developers, Now What?
cornelia davis
 

What's hot (20)

PDF
Consumer-Driven Contract Testing - Workshop - January 2021
Paulo Clavijo
 
PDF
Unleashing Docker with Pipelines in Bitbucket Cloud
Atlassian
 
PDF
Tech talk specflow_bddx_hassa_nagy
Skills Matter
 
PPTX
What's new in Spring Boot 2.0
VMware Tanzu
 
PDF
Peering Inside the Black Box: A Case for Observability
VMware Tanzu
 
PDF
Continuous delivery with Spring Cloud Pipelines Case Study
Kamil Kochański
 
PDF
The 36th Chamber of Shaolin - Improve Your Microservices Kung Fu in 36 Easy S...
Stefan Richter
 
PDF
Not Just Initializing
VMware Tanzu
 
PDF
Principles of microservices velocity
Sam Newman
 
PDF
Prod-Like Integration Testing for Distributed Containerized Applications
VMware Tanzu
 
PDF
Going Cloud Native
David Schmitz
 
PPTX
MVC3 Development with visual studio 2010
AbhishekLuv Kumar
 
PDF
BDD Scenarios in a Testing & Traceability Strategy (Webinar 19/02/2021)
Gáspár Nagy
 
PDF
Continuous integration and delivery for java based web applications
Sunil Dalal
 
PDF
Arquillian & Citrus
christophd
 
PDF
To Microservices and Beyond
Matt Stine
 
PDF
How To Be a Java Automated Testing Superstar
VMware Tanzu
 
PDF
Alon Fliess: APM – What Is It, and Why Do I Need It? - Architecture Next 20
CodeValue
 
PDF
Serverless Spring
VMware Tanzu
 
PDF
Software Supply Chains for DevOps @ InfoQ Live 2021
Aysylu Greenberg
 
Consumer-Driven Contract Testing - Workshop - January 2021
Paulo Clavijo
 
Unleashing Docker with Pipelines in Bitbucket Cloud
Atlassian
 
Tech talk specflow_bddx_hassa_nagy
Skills Matter
 
What's new in Spring Boot 2.0
VMware Tanzu
 
Peering Inside the Black Box: A Case for Observability
VMware Tanzu
 
Continuous delivery with Spring Cloud Pipelines Case Study
Kamil Kochański
 
The 36th Chamber of Shaolin - Improve Your Microservices Kung Fu in 36 Easy S...
Stefan Richter
 
Not Just Initializing
VMware Tanzu
 
Principles of microservices velocity
Sam Newman
 
Prod-Like Integration Testing for Distributed Containerized Applications
VMware Tanzu
 
Going Cloud Native
David Schmitz
 
MVC3 Development with visual studio 2010
AbhishekLuv Kumar
 
BDD Scenarios in a Testing & Traceability Strategy (Webinar 19/02/2021)
Gáspár Nagy
 
Continuous integration and delivery for java based web applications
Sunil Dalal
 
Arquillian & Citrus
christophd
 
To Microservices and Beyond
Matt Stine
 
How To Be a Java Automated Testing Superstar
VMware Tanzu
 
Alon Fliess: APM – What Is It, and Why Do I Need It? - Architecture Next 20
CodeValue
 
Serverless Spring
VMware Tanzu
 
Software Supply Chains for DevOps @ InfoQ Live 2021
Aysylu Greenberg
 
Ad

Similar to Consumer driven contract testing (20)

PPTX
Consumer Driven Contracts for microservices
Reshmi Krishna
 
PDF
TDD for Microservices
VMware Tanzu
 
PDF
Consumer Driven Contracts and Your Microservice Architecture
Marcin Grzejszczak
 
PDF
Spring Cloud Contract And Your Microservice Architecture
Marcin Grzejszczak
 
PDF
Consumer Driven Contracts and Your Microservice Architecture
Marcin Grzejszczak
 
PDF
Consumer Driven Contracts To Enable API Evolution @Geecon
Marcin Grzejszczak
 
PPTX
Consumer driven contracts in java world
Yura Nosenko
 
PPTX
[TestWarez 2017] Behavior Driven Development in a complex environment - Consu...
Stowarzyszenie Jakości Systemów Informatycznych (SJSI)
 
PPTX
Consumer Driven Contracts and Your Microservice Architecture
VMware Tanzu
 
PDF
Consumer Driven Contracts and Your Microservice Architecture
Marcin Grzejszczak
 
PDF
4Developers 2015: Stick to the rules - Consumer Driven Contracts - Marcin Grz...
PROIDEA
 
PDF
Consumer Driven Contracts - 4Developers 2015
Jakub Kubrynski
 
PDF
Contract Testing
kloia
 
PDF
Consumer Driven Contracts like TDD to the API - Olga Maciaszek-Sharma & Marci...
VMware Tanzu
 
PDF
Consumer driven Contract Test
Knoldus Inc.
 
PPTX
Consumer Driven Contracts
Visuality
 
PDF
Microservices: Consumer Driven Contracts in Practice
Qaiser Mazhar
 
PDF
Microservices erfolgreich testen mit Consumer Driven Contract Testing und Spr...
Frank Scheffler
 
PPTX
Contract testing: Beyond API functional testing
Gaurav Singh
 
PDF
SpringOne 2016 in a nutshell
Jeroen Resoort
 
Consumer Driven Contracts for microservices
Reshmi Krishna
 
TDD for Microservices
VMware Tanzu
 
Consumer Driven Contracts and Your Microservice Architecture
Marcin Grzejszczak
 
Spring Cloud Contract And Your Microservice Architecture
Marcin Grzejszczak
 
Consumer Driven Contracts and Your Microservice Architecture
Marcin Grzejszczak
 
Consumer Driven Contracts To Enable API Evolution @Geecon
Marcin Grzejszczak
 
Consumer driven contracts in java world
Yura Nosenko
 
[TestWarez 2017] Behavior Driven Development in a complex environment - Consu...
Stowarzyszenie Jakości Systemów Informatycznych (SJSI)
 
Consumer Driven Contracts and Your Microservice Architecture
VMware Tanzu
 
Consumer Driven Contracts and Your Microservice Architecture
Marcin Grzejszczak
 
4Developers 2015: Stick to the rules - Consumer Driven Contracts - Marcin Grz...
PROIDEA
 
Consumer Driven Contracts - 4Developers 2015
Jakub Kubrynski
 
Contract Testing
kloia
 
Consumer Driven Contracts like TDD to the API - Olga Maciaszek-Sharma & Marci...
VMware Tanzu
 
Consumer driven Contract Test
Knoldus Inc.
 
Consumer Driven Contracts
Visuality
 
Microservices: Consumer Driven Contracts in Practice
Qaiser Mazhar
 
Microservices erfolgreich testen mit Consumer Driven Contract Testing und Spr...
Frank Scheffler
 
Contract testing: Beyond API functional testing
Gaurav Singh
 
SpringOne 2016 in a nutshell
Jeroen Resoort
 
Ad

Recently uploaded (20)

PPTX
The Future of AI & Machine Learning.pptx
pritsen4700
 
PDF
SparkLabs Primer on Artificial Intelligence 2025
SparkLabs Group
 
PDF
GDG Cloud Munich - Intro - Luiz Carneiro - #BuildWithAI - July - Abdel.pdf
Luiz Carneiro
 
PDF
Presentation about Hardware and Software in Computer
snehamodhawadiya
 
PDF
MASTERDECK GRAPHSUMMIT SYDNEY (Public).pdf
Neo4j
 
PDF
Automating ArcGIS Content Discovery with FME: A Real World Use Case
Safe Software
 
PDF
Orbitly Pitch Deck|A Mission-Driven Platform for Side Project Collaboration (...
zz41354899
 
PDF
Using Anchore and DefectDojo to Stand Up Your DevSecOps Function
Anchore
 
PPTX
Agile Chennai 18-19 July 2025 Ideathon | AI Powered Microfinance Literacy Gui...
AgileNetwork
 
PDF
AI Unleashed - Shaping the Future -Starting Today - AIOUG Yatra 2025 - For Co...
Sandesh Rao
 
PDF
Oracle AI Vector Search- Getting Started and what's new in 2025- AIOUG Yatra ...
Sandesh Rao
 
PDF
Data_Analytics_vs_Data_Science_vs_BI_by_CA_Suvidha_Chaplot.pdf
CA Suvidha Chaplot
 
PDF
Trying to figure out MCP by actually building an app from scratch with open s...
Julien SIMON
 
PPTX
Dev Dives: Automate, test, and deploy in one place—with Unified Developer Exp...
AndreeaTom
 
PDF
Brief History of Internet - Early Days of Internet
sutharharshit158
 
PDF
Responsible AI and AI Ethics - By Sylvester Ebhonu
Sylvester Ebhonu
 
PDF
Doc9.....................................
SofiaCollazos
 
PDF
Research-Fundamentals-and-Topic-Development.pdf
ayesha butalia
 
PDF
Accelerating Oracle Database 23ai Troubleshooting with Oracle AHF Fleet Insig...
Sandesh Rao
 
PPTX
AI in Daily Life: How Artificial Intelligence Helps Us Every Day
vanshrpatil7
 
The Future of AI & Machine Learning.pptx
pritsen4700
 
SparkLabs Primer on Artificial Intelligence 2025
SparkLabs Group
 
GDG Cloud Munich - Intro - Luiz Carneiro - #BuildWithAI - July - Abdel.pdf
Luiz Carneiro
 
Presentation about Hardware and Software in Computer
snehamodhawadiya
 
MASTERDECK GRAPHSUMMIT SYDNEY (Public).pdf
Neo4j
 
Automating ArcGIS Content Discovery with FME: A Real World Use Case
Safe Software
 
Orbitly Pitch Deck|A Mission-Driven Platform for Side Project Collaboration (...
zz41354899
 
Using Anchore and DefectDojo to Stand Up Your DevSecOps Function
Anchore
 
Agile Chennai 18-19 July 2025 Ideathon | AI Powered Microfinance Literacy Gui...
AgileNetwork
 
AI Unleashed - Shaping the Future -Starting Today - AIOUG Yatra 2025 - For Co...
Sandesh Rao
 
Oracle AI Vector Search- Getting Started and what's new in 2025- AIOUG Yatra ...
Sandesh Rao
 
Data_Analytics_vs_Data_Science_vs_BI_by_CA_Suvidha_Chaplot.pdf
CA Suvidha Chaplot
 
Trying to figure out MCP by actually building an app from scratch with open s...
Julien SIMON
 
Dev Dives: Automate, test, and deploy in one place—with Unified Developer Exp...
AndreeaTom
 
Brief History of Internet - Early Days of Internet
sutharharshit158
 
Responsible AI and AI Ethics - By Sylvester Ebhonu
Sylvester Ebhonu
 
Doc9.....................................
SofiaCollazos
 
Research-Fundamentals-and-Topic-Development.pdf
ayesha butalia
 
Accelerating Oracle Database 23ai Troubleshooting with Oracle AHF Fleet Insig...
Sandesh Rao
 
AI in Daily Life: How Artificial Intelligence Helps Us Every Day
vanshrpatil7
 

Consumer driven contract testing