TDD for Microservices
By Reshmi Krishna By Vinay Upadhya
@reshmi9k @vinayu
About Us
Reshmi Krishna
●  Software Engineer
●  Platform Architect, Pivotal Cloud Foundry
●  Women In Tech Community Member
Twitter : @reshmi9k
MeetUp : Cloud-Native-New-York
Vinay Upadhya
●  Software Engineer
●  Platform Architect, Pivotal Cloud Foundry
Twitter : @vinayu
Agenda
Testing Pyramid
What are Consumer Driven Contracts?
What problems are we trying to solve?
Spring Cloud Contract for microservices
Demo
Testing Pyramid
https://martinfowler.com/articles/microservice-testing/#conclusion-test-pyramid
What are Consumer Driven Contracts?
Internal
Marketing
App
External
Reseller
App
Product
Search
Service
A
P
I
{ catalogid, name,
price,
manufacturer }
{ catalogid,
name, price }
Product Schema
{ catalogid, name, price,
manufacturer,
inStock }
Producer
Contract
Expectation
Consumer
Contract
Consumer Driven
Contract
What does a contract look like?
What problems are we trying to solve?
1.  Provide a framework for consumers to set
expectations using contracts with the
producer.
2.  Have the producer own the stub generation
based on the consumer contracts to maintain
consistency.
3.  Have the consumer build their application
confidently using TDD against the producer
validated stubs.
4.  Producer test lifecycle includes validating
consumer contracts.
5.  Minimize E2E integration tests.
Spring Cloud Contract
Spring Cloud Contract provides support for CDC and service schemas in Spring Apps.
Being part of Spring Cloud it can help stub out Spring Cloud components like Eureka,
Consul etc.
Comprised of three main projects :-
Spring Cloud Contract Verifier : Library on server side to test if the server isn’t breaking
any consumer contracts.
Spring Cloud Contract Wiremock : Wrapper around wiremock to help start stubs with
wiremock json stubs.
Spring Cloud Contract Stub Runner : Helper library to pull stubs from maven and run
Demo
I want to login
Can springdays/123 login ?
OK
OK
I want to login Can springdays1/123/123 login?
OKOK
Login Service
Demo
I want to login
Can Springdays/321 login?
NOT_OKAY
NO
I want to login Can springdays1/123/123as login?
NONOT_OKAY
Login Service
Typical CDC Flow
Consumer Phase 1 Producer Phase Consumer Phase 2
Consumer
Offline Work
Producer
Implementing
Feature
Consumer
Online Work
Consumer Flow 1 (Login Mobile Client)
Start TDD - writes the test for the feature
Write the missing implementation
Clone the Producer (LoginService) repository locally
Define the contract locally in the repo of Producer
Add the Spring Cloud Contract Verifier plugin
Run the integration tests
Producer Flow (Login Server)
Take over the PR
Write the missing implementation that will make the autogenerated tests pass
Merge PR and deploys the JARs with the app and the stubs
Consumer Flow 2 (Login Mobile Client)
Switches off the Stub Runner’s offline mode once the producer uploads the
stubs
Configure Stub Runner by providing the URL to a repo with stubs
Will have its test broken if the producer makes any breaking changes of the API
Resources
Docs : https://cloud.spring.io/spring-cloud-contract/spring-cloud-contract.html
MeetUp Demo : https://github.com/reshmik/spring-cloud-contract-login-demo.git
Consumer Driven Contracts (MarcinGrzejszczak) :
http://www.slideshare.net/MarcinGrzejszczak/consumer-driven-contracts-and-your-microservice-architecture-67641511
More Samples : https://github.com/spring-cloud/spring-cloud-contract/tree/1.0.x/samples
Talk to the SpringCloudContract Team : https://gitter.im/spring-cloud/spring-cloud-contract

TDD for Microservices

  • 1.
    TDD for Microservices ByReshmi Krishna By Vinay Upadhya @reshmi9k @vinayu
  • 2.
    About Us Reshmi Krishna ● Software Engineer ●  Platform Architect, Pivotal Cloud Foundry ●  Women In Tech Community Member Twitter : @reshmi9k MeetUp : Cloud-Native-New-York Vinay Upadhya ●  Software Engineer ●  Platform Architect, Pivotal Cloud Foundry Twitter : @vinayu
  • 3.
    Agenda Testing Pyramid What areConsumer Driven Contracts? What problems are we trying to solve? Spring Cloud Contract for microservices Demo
  • 4.
  • 5.
    What are ConsumerDriven Contracts? Internal Marketing App External Reseller App Product Search Service A P I { catalogid, name, price, manufacturer } { catalogid, name, price } Product Schema { catalogid, name, price, manufacturer, inStock } Producer Contract Expectation Consumer Contract Consumer Driven Contract
  • 6.
    What does acontract look like?
  • 7.
    What problems arewe trying to solve? 1.  Provide a framework for consumers to set expectations using contracts with the producer. 2.  Have the producer own the stub generation based on the consumer contracts to maintain consistency. 3.  Have the consumer build their application confidently using TDD against the producer validated stubs. 4.  Producer test lifecycle includes validating consumer contracts. 5.  Minimize E2E integration tests.
  • 8.
    Spring Cloud Contract SpringCloud Contract provides support for CDC and service schemas in Spring Apps. Being part of Spring Cloud it can help stub out Spring Cloud components like Eureka, Consul etc. Comprised of three main projects :- Spring Cloud Contract Verifier : Library on server side to test if the server isn’t breaking any consumer contracts. Spring Cloud Contract Wiremock : Wrapper around wiremock to help start stubs with wiremock json stubs. Spring Cloud Contract Stub Runner : Helper library to pull stubs from maven and run
  • 9.
    Demo I want tologin Can springdays/123 login ? OK OK I want to login Can springdays1/123/123 login? OKOK Login Service
  • 10.
    Demo I want tologin Can Springdays/321 login? NOT_OKAY NO I want to login Can springdays1/123/123as login? NONOT_OKAY Login Service
  • 11.
    Typical CDC Flow ConsumerPhase 1 Producer Phase Consumer Phase 2 Consumer Offline Work Producer Implementing Feature Consumer Online Work
  • 12.
    Consumer Flow 1(Login Mobile Client) Start TDD - writes the test for the feature Write the missing implementation Clone the Producer (LoginService) repository locally Define the contract locally in the repo of Producer Add the Spring Cloud Contract Verifier plugin Run the integration tests
  • 13.
    Producer Flow (LoginServer) Take over the PR Write the missing implementation that will make the autogenerated tests pass Merge PR and deploys the JARs with the app and the stubs
  • 14.
    Consumer Flow 2(Login Mobile Client) Switches off the Stub Runner’s offline mode once the producer uploads the stubs Configure Stub Runner by providing the URL to a repo with stubs Will have its test broken if the producer makes any breaking changes of the API
  • 15.
    Resources Docs : https://cloud.spring.io/spring-cloud-contract/spring-cloud-contract.html MeetUpDemo : https://github.com/reshmik/spring-cloud-contract-login-demo.git Consumer Driven Contracts (MarcinGrzejszczak) : http://www.slideshare.net/MarcinGrzejszczak/consumer-driven-contracts-and-your-microservice-architecture-67641511 More Samples : https://github.com/spring-cloud/spring-cloud-contract/tree/1.0.x/samples Talk to the SpringCloudContract Team : https://gitter.im/spring-cloud/spring-cloud-contract