Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Consumer Driven Contract Testing with Spring Cloud Contract

Consumer Driven Contract Testing with Spring Cloud Contract

Eddú Meléndez Gonzales

September 25, 2018
Tweet

More Decks by Eddú Meléndez Gonzales

Other Decks in Programming

Transcript

  1. Unless otherwise indicated, these slides are © 2013-2018 Pivotal Software,

    Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ About us Olga Maciaszek-Sharma Software Engineer, Spring Cloud Team, Spring Project Twitter: @olga_maciaszek
  2. Unless otherwise indicated, these slides are © 2013-2018 Pivotal Software,

    Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ About us Eddú Meléndez Software Engineer and Open Source Contributor Twitter: @EdduMelendez
  3. Unless otherwise indicated, these slides are © 2013-2018 Pivotal Software,

    Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Software Architecture Evolution
  4. Unless otherwise indicated, these slides are © 2013-2018 Pivotal Software,

    Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Software Architecture Evolution - Monolith Browser Application Web Service
  5. Unless otherwise indicated, these slides are © 2013-2018 Pivotal Software,

    Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Software Architecture Evolution - Microservices Consumer 1 API Gateway Service 2 Service 3 Service 1 Consumer 2
  6. Unless otherwise indicated, these slides are © 2013-2018 Pivotal Software,

    Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ "Organizations which design systems ... are constrained to produce designs which are copies of the communication structures of these organizations." — M. Conway
  7. Unless otherwise indicated, these slides are © 2013-2018 Pivotal Software,

    Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Microservices
  8. Unless otherwise indicated, these slides are © 2013-2018 Pivotal Software,

    Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Microservices 9
  9. Unless otherwise indicated, these slides are © 2013-2018 Pivotal Software,

    Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ What we need to solve before doing microservices? • Deployment • APIs • Contracts • Testing • Service Discovery • Health Checking • Configuration • Circuit Breaking • Tracing • Logging • Monitoring • ….
  10. Unless otherwise indicated, these slides are © 2013-2018 Pivotal Software,

    Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ What we need to solve before doing microservices? • Deployment • APIs • Contracts • Testing • Service Discovery • Health Checking • Configuration • Circuit Breaking • Tracing • Logging • Monitoring • ….
  11. Unless otherwise indicated, these slides are © 2013-2018 Pivotal Software,

    Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Pain points during development • Unstable development environment ◦ Service not available • Downtime during deployments • Data is not available • Finding bugs during functional tests
  12. Unless otherwise indicated, these slides are © 2013-2018 Pivotal Software,

    Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Testing Microservices
  13. Unless otherwise indicated, these slides are © 2013-2018 Pivotal Software,

    Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Test Pyramid 14 Source: https://testing.googleblog.com/2015/04/just-say-no-to-more-end-to-end-tests.html
  14. Unless otherwise indicated, these slides are © 2013-2018 Pivotal Software,

    Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ 1 5  Fully Deployed Test Environment Environment per Service One To Rule Them All
  15. Unless otherwise indicated, these slides are © 2013-2018 Pivotal Software,

    Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ 1 6  Deployment Coupling
  16. Unless otherwise indicated, these slides are © 2013-2018 Pivotal Software,

    Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ 1 7  Failsafe vs. Safe To Fail
  17. Unless otherwise indicated, these slides are © 2013-2018 Pivotal Software,

    Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Circuit Breakers 18
  18. Unless otherwise indicated, these slides are © 2013-2018 Pivotal Software,

    Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ 1 9  Stubbed Environments
  19. Unless otherwise indicated, these slides are © 2013-2018 Pivotal Software,

    Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Invalid Stubs - REST 2 0 { “request” : { “method”: “GET”, “url” : “/endpointX”, }, “response” : { “status” : 200 } { “request” : { “method”: “GET”, “url” : “/endpointY”, }, “response” : { “status” : 200 }
  20. Unless otherwise indicated, these slides are © 2013-2018 Pivotal Software,

    Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Invalid Stubs - Messaging 21 topic: foo class Product { int productId String productName } topic: bar class Product { int productId String productName } topic: foo class Product { int id String productName }
  21. Unless otherwise indicated, these slides are © 2013-2018 Pivotal Software,

    Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ 2 2  Outdated Stubs
  22. Unless otherwise indicated, these slides are © 2013-2018 Pivotal Software,

    Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Consumer Driven Contracts
  23. Unless otherwise indicated, these slides are © 2013-2018 Pivotal Software,

    Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Terminology • Producer • Consumer • Contract
  24. Unless otherwise indicated, these slides are © 2013-2018 Pivotal Software,

    Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Spring Cloud Contract Benefits From Producer side: • Nice DSL to define contracts • Generate Stubs • Generate tests against defined contracts
  25. Unless otherwise indicated, these slides are © 2013-2018 Pivotal Software,

    Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Spring Cloud Contract Benefits From Consumer side: • Validation against Contracts • Run stubs generated thanks to StubRunner
  26. Unless otherwise indicated, these slides are © 2013-2018 Pivotal Software,

    Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Test using MockRestServiceServer 2 7 @RestClientTest public class MockServerTests { @Autowired private MockRestServiceServer server; @Test public void shouldReturnAllItems() { this.server.expect(requestTo(...uri...)) .andResponse(withSuccess(...body..., ...media type...)); } … }
  27. Unless otherwise indicated, these slides are © 2013-2018 Pivotal Software,

    Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Test using WireMock 2 8 @AutoConfigureWireMock public class WireMockTests { @Test public void shouldReturnAllItems() { stub.get(urlEqualTo(...uri...)) .willReturn(aResponse() .withHeader(...key..., ...value...) .withBody(...body...))); } … }
  28. Unless otherwise indicated, these slides are © 2013-2018 Pivotal Software,

    Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Demo
  29. Unless otherwise indicated, these slides are © 2013-2018 Pivotal Software,

    Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ ATDD & Backwards Compatible APIs
  30. Unless otherwise indicated, these slides are © 2013-2018 Pivotal Software,

    Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Architecture TDD 31
  31. Unless otherwise indicated, these slides are © 2013-2018 Pivotal Software,

    Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Backwards Compatibility Check 3 2
  32. Unless otherwise indicated, these slides are © 2013-2018 Pivotal Software,

    Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Deferred Updates 3 3
  33. Unless otherwise indicated, these slides are © 2013-2018 Pivotal Software,

    Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Current Status - 2.0.1-RELEASE • Supports Messaging (Integration, Stream, RabbitMQ, Kafka) • Works with MockMvc • Works with RestDocs • Support for Maven and Gradle • Provides StubRunner • JUnit Rules • Pact Support
  34. Unless otherwise indicated, these slides are © 2013-2018 Pivotal Software,

    Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ The Future
  35. Unless otherwise indicated, these slides are © 2013-2018 Pivotal Software,

    Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ What is coming? - 2.1.0 • JUnit 5 extension for Spring Cloud Contract - Issue 691 • WebTestClient support (WebFlux support) - Issue 422 Link: https://bit.ly/2O0HSHc