testing java microservices with consumer driven contracts
play

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


  1. Testing Java Microservices with Consumer-driven contracts Andrew Morgan @mogronalol

  2. Microservices testing is hard

  3. Consumer driven contract testing

  4. About Me • Independent Consultant specialising in microservices & continuous delivery • PluralSight Author • InfoQ Editor Andrew Morgan @mogronalol

  5. Agenda • Introducing microservices testing challenges • Consumer-driven contract testing overview • Following through the workflow with Spring Cloud Contract • Advanced consumer-driven contract concepts

  6. Agenda • Introducing microservices testing challenges • Consumer-driven contract testing overview • Following through the workflow with Spring Cloud Contract • Advanced consumer-driven contract concepts

  7. What are Microservices? Rewards Accounts Payments Loans

  8. What are Microservices? Rewards HTTP Accounts HTTP Payments Loans

  9. What are Microservices? Rewards HTTP Accounts Messaging HTTP Payments Loans

  10. The root cause of testing challenges REST API Booking Payments

  11. End-to-end Testing Microservices Automated tests Browser or other UI Environment

  12. Disadvantages of End-to-end Testing Build time Debuggability Infrastructure cost Operations Flakiness Distributed monolith

  13. Mocking or Stubbing Dependencies Automated tests Microservice Mock or Stub of Dependency

  14. Disadvantages of Mocking or Stubbing False positives Staleness Difficult to maintain

  15. The Weak Feedback Loop Run individual Build and deploy microservice Release to stage End-to-end test tests Slow and flaky False positives

  16. Agenda • Introducing microservices testing challenges • Consumer-driven contract testing overview • Following through the workflow with Spring Cloud Contract • Advanced consumer-driven contract concepts

  17. 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

  18. Test Architecture Overview ??? Consumer Provider

  19. Key Contract Characteristics An agreed interaction between microservices Continuously tested No specific protocol Not the same stubbing! Definitely not API documentation!

  20. What is an Interaction? Scenario: Should return no flights between London and Paris

  21. What is an Interaction? Scenario: Should return no flights between London and Paris Required Request: GET /flights?from=London&to=Paris

  22. What is an Interaction? Scenario: Should return no flights between London and Paris Required Request: GET /flights?from=London&to=Paris Given Response: 200 []

  23. The Consumer Side Automated Consuming Stub of provider tests Microservice microservice Generates The interaction our test depends on

  24. The Provider Side Providing Contract verification Microservice tests Generates Pre-defined contract

  25. The Benefits 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

  26. Agenda • Introducing microservices testing challenges • Consumer-driven contract testing overview • Following through the workflow with Spring Cloud Contract • Advanced consumer-driven contract concepts

  27. Spring Cloud Contract Overview JVM-based CDC framework Spring Boot Integration Groovy Based Contract Definition Language Message-driven and HTTP-driven interactions

  28. Credit Card Application Feature

  29. Credit Card Application Feature JUnit Test Credit card Credit score service applications service

  30. Credit Card Application Feature JUnit Test Credit card Credit score service applications service Applies for credit card

  31. Credit Card Application Feature JUnit Test Credit card Credit score service applications service Calculates credit score

  32. Credit Card Application Feature JUnit Test Credit card Credit score service applications service Accepts or rejects based on credit score

  33. Following the CDC Workflow Write a Define Build Make Build Make tests failing test Contract stubs tests verification pass pass tests

  34. Following the CDC Workflow Write a failing test

  35. A Failing Test

  36. A Failing Test

  37. A Failing Test

  38. Implementing Our Endpoint JUnit Test

  39. Implementing Our Endpoint JUnit Test Credit card applications service

  40. Implementing Our Endpoint JUnit Test Credit card Credit score service applications service Does not exist

  41. Implementing Our Endpoint JUnit Test Credit card Credit score service applications service Fails here

  42. Following the CDC Workflow Write a Define failing test Contract

  43. Contract Definition Language Request made by our test Response expected by our test

  44. Following the CDC Workflow Write a Define Build failing test Contract stubs

  45. Stub Generation Contract Spring Cloud Stub Jar Contract Plugin

  46. wiremock.org

  47. Following the CDC Workflow Write a Define Build Make failing test Contract stubs tests pass

  48. The Stub Runner Download given stubs Boots WireMock Imports stubs into Wiremock

  49. From a Failing Test Unit test Credit card Credit score applications service service Fails here

  50. …to a Passing Test Unit test Credit card Spring Cloud applications service Contract Stub Passes here

  51. Following the CDC Workflow Write a Define Build Make Build failing test Contract stubs tests verification pass tests

  52. Provider Test Generation Contract Spring Cloud Verification Contract Plugin Tests

  53. Following the CDC Workflow Write a Define Build Make Build Make tests failing test Contract stubs tests verification pass pass tests

  54. From Failing… Contract Verification Credit score Tests service

  55. To Passing… Contract Verification Credit score Tests service Implement me

  56. “Consumer-driven contracts are like TDD applied at the API level.” - Marcin Grzejszczak

  57. Agenda • Introducing microservices testing challenges • Consumer-driven contract testing overview • Following through the workflow with Spring Cloud Contract • Advanced consumer-driven contract concepts

  58. Does this only work with exact comparisons?

  59. Subsets Regex XPath JSON XML Ignore casing Starts with More

  60. What about messaging?

  61. Message-driven CDC Consumer Provider

  62. Message-driven CDC Consumer Broker Provider

  63. Why Messaging? Message schemas and queues are shared Asynchronous failures are a debugging nightmare! Brokers are slow

  64. Where do we store our contracts?

  65. Contract Repository

  66. Contract Repository

  67. Talk Summary Don’t build and test microservices like a monolith Consumer-driven contracts promote independence Leverage frameworks like Spring Cloud Contract and Pact

  68. Questions? https://www.pluralsight.com/profile/author/andrew-morgan @mogronalol

Recommend


More recommend