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

Contract testing with Pact and Quarkus

Contract testing with Pact and Quarkus

Every time you change one microservice, the others break. But you had unit tests! What’s going on? Unit tests aren’t enough to give system-level confidence, even with a microservices architecture. What’s the solution? Integration testing is annoyingly expensive, flaky, and fries your laptop. Remocal development environments are complex to manage. Testing in production is important, but the feedback loop is longer, so it’s not a substitute for local inner-loop testing. Enter contract tests. Contract tests combine the lightness of unit tests with the confidence of integration tests, and should be part of your development toolkit. This session will demo how to use the Pact contract testing framework to catch integration issues early. The demos will be run on Quarkus, but Pact runs on all Java frameworks. In fact, Pact has libraries for almost all of the popular programming languages.

Holly Cummins

May 03, 2023
Tweet

More Decks by Holly Cummins

Other Decks in Programming

Transcript

  1. @holly_cummins “how do I know the whole system works? …

    without losing the goodness of microservices?” the big microservices question
  2. @holly_cummins #RedHat “we made our pipeline enforce releasing all our

    microservices at the same time.” true story
  3. @holly_cummins #RedHat “we made our pipeline enforce releasing all our

    microservices at the same time.” true story are you dependent on end-to-end testing?
  4. @holly_cummins #RedHat our code our mock our code their actual

    code tests ✔ why mocks aren’t enough
  5. @holly_cummins #RedHat our code our mock our code their actual

    code tests ✔ reality ✘ why mocks aren’t enough
  6. @holly_cummins #RedHat our code their code contract test mock functional

    test our tests ✔ their tests ✔ reality ✔ why mocks aren’t enough
  7. @holly_cummins #RedHat our code their code contract test mock functional

    test our tests ✔ their tests ✘ reality ✘ why mocks aren’t enough
  8. @holly_cummins #RedHat our code their code contract test mock functional

    test our tests ✘ their tests ✔ reality ✘ why mocks aren’t enough
  9. @holly_cummins #RedHat if you remember one thing … • don’t

    test the mock! • your consumer test should be testing your code, not what’s coming back from the other side • lots of examples get this wrong :(
  10. @holly_cummins #RedHat cold person knitter sheep farmer semantics vs syntax

    (why sharing expectations about behaviour is important)
  11. @holly_cummins #RedHat cold person knitter sheep farmer “white sweater, please”

    semantics vs syntax (why sharing expectations about behaviour is important)
  12. @holly_cummins #RedHat cold person knitter sheep farmer “white sweater, please”

    semantics vs syntax (why sharing expectations about behaviour is important)
  13. @holly_cummins #RedHat cold person white sweater knitter sheep farmer “white

    sweater, please” semantics vs syntax (why sharing expectations about behaviour is important)
  14. @holly_cummins #RedHat cold person white sweater knitter sheep farmer “white

    sweater, please” white wool semantics vs syntax (why sharing expectations about behaviour is important)
  15. @holly_cummins #RedHat cold person white sweater knitter sheep farmer “white

    sweater, please” “black sweater, please” white wool semantics vs syntax (why sharing expectations about behaviour is important)
  16. @holly_cummins #RedHat cold person white sweater knitter sheep farmer “white

    sweater, please” “black sweater, please” white wool semantics vs syntax (why sharing expectations about behaviour is important)
  17. @holly_cummins #RedHat cold person white sweater knitter sheep farmer “white

    sweater, please” “black sweater, please” white wool black sweater semantics vs syntax (why sharing expectations about behaviour is important)
  18. @holly_cummins #RedHat cold person white sweater knitter sheep farmer “white

    sweater, please” “black sweater, please” white wool black sweater black wool semantics vs syntax (why sharing expectations about behaviour is important)
  19. @holly_cummins #RedHat cold person white sweater knitter sheep farmer “white

    sweater, please” “black sweater, please” “pink sweater, please” white wool black sweater black wool semantics vs syntax (why sharing expectations about behaviour is important)
  20. @holly_cummins #RedHat cold person white sweater knitter sheep farmer “white

    sweater, please” “black sweater, please” “pink sweater, please” white wool black sweater black wool semantics vs syntax (why sharing expectations about behaviour is important)
  21. @holly_cummins #RedHat cold person white sweater knitter sheep farmer “white

    sweater, please” “black sweater, please” “pink sweater, please” white wool black sweater black wool pink sweater semantics vs syntax (why sharing expectations about behaviour is important)
  22. @holly_cummins #RedHat cold person white sweater knitter sheep farmer “white

    sweater, please” “black sweater, please” “pink sweater, please” white wool black sweater black wool pink sweater pink wool semantics vs syntax (why sharing expectations about behaviour is important)
  23. @holly_cummins #RedHat cold person white sweater knitter sheep farmer “white

    sweater, please” “black sweater, please” “pink sweater, please” white wool black sweater black wool pink sweater pink wool semantics vs syntax (why sharing expectations about behaviour is important) what?! pink sheep don’t exist
  24. @holly_cummins #RedHat cold person white sweater knitter sheep farmer “white

    sweater, please” “black sweater, please” “pink sweater, please” white wool black sweater black wool pink sweater pink wool semantics vs syntax (why sharing expectations about behaviour is important) should the knitter ask for white wool and bring in a dyeing service? what?! pink sheep don’t exist
  25. @holly_cummins #RedHat cold person white sweater knitter sheep farmer “white

    sweater, please” “black sweater, please” “pink sweater, please” white wool black sweater black wool pink sweater pink wool semantics vs syntax (why sharing expectations about behaviour is important) should the knitter ask for white wool and bring in a dyeing service? should the farmer dye the wool? what?! pink sheep don’t exist
  26. @holly_cummins #RedHat Photo of sheep at the Latitude Festival by

    Tim Parkinson, https://www.flickr.com/photos/timparkinson
  27. @holly_cummins #RedHat Photo of sheep at the Latitude Festival by

    Tim Parkinson, https://www.flickr.com/photos/timparkinson “pink sheep don’t exist”
  28. @holly_cummins #RedHat how annoying, the provider is returning 204. the

    contract expects 404, so my test is broken. quarkus will return 204 by default. 204 vs 404 - it’s more about the theory behind REST...if a caller asks for a resource the server doesn't have, it should be a 404 - same way if a user types in an invalid URL into the browser it’s too late at night to care about REST theory. I’m just going to update the expectations in the test to match reality. what?! why is my other contract test broken now? 
 oh … because what I’m changing is a mock…. Based on a true story….
  29. @holly_cummins #RedHat contract consumer tests provider mock Pact contract tests

    verify Pact (functional tests) contract testing landscape
  30. @holly_cummins #RedHat contract consumer tests provider mock Pact declare (Pact)

    contract tests verify Pact (functional tests) contract testing landscape Pact file
  31. @holly_cummins #RedHat contract consumer tests provider mock Pact declare (Pact)

    contract tests verify Pact (functional tests) OpenAPI contract testing landscape Pact file
  32. @holly_cummins #RedHat contract consumer tests provider mock Pact declare (Pact)

    contract tests verify Pact (functional tests) OpenAPI OpenAPI + Schemathesis (syntax only, weaker connection to consumer expectations) contract testing landscape Pact file
  33. @holly_cummins #RedHat contract consumer tests provider mock Pact declare (Pact)

    contract tests verify Pact (functional tests) OpenAPI OpenAPI + Schemathesis (syntax only, weaker connection to consumer expectations) contract testing landscape Pact file Microcks
  34. @holly_cummins #RedHat contract consumer tests provider mock Pact Prism (OpenAPI)

    declare (Pact) contract tests verify Pact (functional tests) OpenAPI OpenAPI + Schemathesis (syntax only, weaker connection to consumer expectations) contract testing landscape Pact file Microcks
  35. @holly_cummins #RedHat contract consumer tests provider mock Pact Prism (OpenAPI)

    Microcks (OpenAPI) declare (Pact) contract tests verify Pact (functional tests) OpenAPI OpenAPI + Schemathesis (syntax only, weaker connection to consumer expectations) contract testing landscape Pact file Microcks
  36. @holly_cummins #RedHat contract consumer tests provider mock Pact Prism (OpenAPI)

    Microcks (OpenAPI) declare (Pact) contract tests verify Pact (functional tests) OpenAPI OpenAPI + Schemathesis (syntax only, weaker connection to consumer expectations) contract testing landscape Pact file mock server OpenAPI + Prism Microcks Microcks
  37. @holly_cummins #RedHat contract consumer tests provider mock Pact Prism (OpenAPI)

    Microcks (OpenAPI) declare (Pact) contract tests verify Pact (functional tests) OpenAPI OpenAPI + Schemathesis (syntax only, weaker connection to consumer expectations) contract testing landscape Pact file generate (OpenAPI snapshot of current implementation) mock server OpenAPI + Prism Microcks Microcks
  38. @holly_cummins #RedHat contract consumer tests provider mock Pact Prism (OpenAPI)

    Microcks (OpenAPI) declare (Pact) generate skeleton from OpenAPI contract tests verify Pact (functional tests) OpenAPI OpenAPI + Schemathesis (syntax only, weaker connection to consumer expectations) contract testing landscape Pact file generate (OpenAPI snapshot of current implementation) mock server OpenAPI + Prism Microcks Microcks
  39. @holly_cummins #RedHat contract consumer tests provider mock Pact Prism (OpenAPI)

    Microcks (OpenAPI) declare (Pact) generate skeleton from OpenAPI contract tests verify Pact (functional tests) OpenAPI OpenAPI + Schemathesis (syntax only, weaker connection to consumer expectations) contract testing landscape Pact file dashboard generate (OpenAPI snapshot of current implementation) mock server OpenAPI + Prism Microcks Microcks
  40. @holly_cummins #RedHat contract consumer tests provider mock Pact Prism (OpenAPI)

    Microcks (OpenAPI) declare (Pact) generate skeleton from OpenAPI contract tests verify Pact (functional tests) OpenAPI OpenAPI + Schemathesis (syntax only, weaker connection to consumer expectations) contract testing landscape Pact file dashboard Pact broker generate (OpenAPI snapshot of current implementation) mock server OpenAPI + Prism Microcks Microcks
  41. @holly_cummins #RedHat contract consumer tests provider mock Pact Prism (OpenAPI)

    Microcks (OpenAPI) declare (Pact) generate skeleton from OpenAPI contract tests verify Pact (functional tests) OpenAPI OpenAPI + Schemathesis (syntax only, weaker connection to consumer expectations) contract testing landscape Pact file dashboard Pact broker Microcks generate (OpenAPI snapshot of current implementation) mock server OpenAPI + Prism Microcks Microcks
  42. @holly_cummins #RedHat contract consumer tests provider mock Pact Prism (OpenAPI)

    Microcks (OpenAPI) declare (Pact) generate skeleton from OpenAPI contract tests verify Pact (functional tests) OpenAPI OpenAPI + Schemathesis (syntax only, weaker connection to consumer expectations) contract testing landscape Pact file dashboard Pact broker Microcks generate (OpenAPI snapshot of current implementation) mock server OpenAPI + Prism Microcks Microcks other protocol specs
  43. @holly_cummins #RedHat contract consumer provider contract tests verify provider-driven contract

    testing generate (OpenAPI snapshot of current implementation)
  44. @holly_cummins #RedHat contract consumer tests provider mock contract tests verify

    provider-driven contract testing generate (OpenAPI snapshot of current implementation)
  45. @holly_cummins #RedHat contract consumer tests provider mock contract tests verify

    OpenAPI provider-driven contract testing generate (OpenAPI snapshot of current implementation)
  46. @holly_cummins #RedHat contract consumer tests provider mock contract tests verify

    OpenAPI provider-driven contract testing generate (OpenAPI snapshot of current implementation) other protocol specs
  47. @holly_cummins #RedHat contract consumer tests provider mock contract tests verify

    OpenAPI provider-driven contract testing generate (OpenAPI snapshot of current implementation) other protocol specs + familiar contract format
  48. @holly_cummins #RedHat contract consumer tests provider mock contract tests verify

    OpenAPI provider-driven contract testing generate (OpenAPI snapshot of current implementation) other protocol specs + familiar contract format + provider does not need comms with consumers
  49. @holly_cummins #RedHat contract consumer tests provider mock contract tests verify

    OpenAPI provider-driven contract testing generate (OpenAPI snapshot of current implementation) other protocol specs + familiar contract format + provider does not need comms with consumers - provider does not get insight into how API is being used
  50. @holly_cummins #RedHat contract consumer tests provider mock contract tests verify

    OpenAPI provider-driven contract testing generate (OpenAPI snapshot of current implementation) other protocol specs + familiar contract format + provider does not need comms with consumers - provider does not get insight into how API is being used - spec-based tests lack depth
  51. @holly_cummins #RedHat contract consumer tests provider mock declare (Pact) contract

    tests verify Pact file consumer-driven contract testing
  52. @holly_cummins #RedHat contract consumer tests provider mock declare (Pact) contract

    tests verify OpenAPI Pact file consumer-driven contract testing
  53. @holly_cummins #RedHat contract consumer tests provider mock declare (Pact) contract

    tests verify OpenAPI Pact file other protocol specs consumer-driven contract testing
  54. @holly_cummins #RedHat contract consumer tests provider mock declare (Pact) contract

    tests verify OpenAPI Pact file other protocol specs consumer-driven contract testing + rich semantic testing
  55. @holly_cummins #RedHat contract consumer tests provider mock declare (Pact) contract

    tests verify OpenAPI Pact file other protocol specs consumer-driven contract testing + rich semantic testing + provider can ‘develop to the test’ instead of developing speculatively
  56. @holly_cummins #RedHat contract consumer tests provider mock declare (Pact) contract

    tests verify OpenAPI Pact file other protocol specs consumer-driven contract testing + rich semantic testing + provider can ‘develop to the test’ instead of developing speculatively - existing spec assets cannot be re-purposed
  57. @holly_cummins #RedHat contract consumer tests provider mock declare (Pact) contract

    tests verify OpenAPI Pact file other protocol specs consumer-driven contract testing + rich semantic testing + provider can ‘develop to the test’ instead of developing speculatively - existing spec assets cannot be re-purposed - provider needs to identify consumers
  58. @holly_cummins #RedHat contract consumer tests provider mock declare (Pact) contract

    tests verify OpenAPI Pact file other protocol specs consumer-driven contract testing + rich semantic testing + provider can ‘develop to the test’ instead of developing speculatively - existing spec assets cannot be re-purposed - provider needs to identify consumers - consumers can break provider CI
  59. @holly_cummins #RedHat some contract testing options • Pact • Microcks

    • Spring Cloud Contract • OpenAPI + schema validation + mock generation • Prism • Schemathesis
  60. Want to know/learn/watch more about Quarkus? • Battle of the

    frameworks : Quarkus vs SpringBoot (11:10-11:50, MC2) • Stream processing app. with Kafka and Quarkus (12:20-13:00, MC2) • Quarkus on AWS Lambda (14:10-14:25, MC3) • Why and when to write a Quarkus extension (14:35-14:50, MC3) • Quarkus Community BOF (15:05-16:05, MC3) ⇨ OpenId Connect and JWT Security in Quarkus (17:30-18:10, MC2)
  61. @holly_cummins #RedHat maturity model for quarkus extensions 1. actually works

    (a) works in JVM mode (b)works in dev mode (c) works in native
  62. @holly_cummins #RedHat maturity model for quarkus extensions 1. actually works

    (a) works in JVM mode (b)works in dev mode (c) works in native 2. developer joy
  63. @holly_cummins #RedHat maturity model for quarkus extensions 1. actually works

    (a) works in JVM mode (b)works in dev mode (c) works in native 2. developer joy (a) use build-time application knowledge to remove boilerplate
  64. @holly_cummins #RedHat maturity model for quarkus extensions 1. actually works

    (a) works in JVM mode (b)works in dev mode (c) works in native 2. developer joy (a) use build-time application knowledge to remove boilerplate 3. developer service (if there is an external service dependency)
  65. @holly_cummins #RedHat maturity model for quarkus extensions 1. actually works

    (a) works in JVM mode (b)works in dev mode (c) works in native 2. developer joy (a) use build-time application knowledge to remove boilerplate 3. developer service (if there is an external service dependency) 4. dev UI
  66. @holly_cummins #RedHat maturity model for quarkus extensions 1. actually works

    (a) works in JVM mode (b)works in dev mode (c) works in native 2. developer joy (a) use build-time application knowledge to remove boilerplate 3. developer service (if there is an external service dependency) 4. dev UI 5. quickstart code template
  67. @holly_cummins #RedHat maturity model for quarkus extensions 1. actually works

    (a) works in JVM mode (b)works in dev mode (c) works in native 2. developer joy (a) use build-time application knowledge to remove boilerplate 3. developer service (if there is an external service dependency) 4. dev UI 5. quickstart code template quarkus-pact