Slide 1

Slide 1 text

Eric Deandrea Java Champion Sr. Principal Developer Advocate Red Hat @edeandrea Drawings courtesy of Holly Cummins Avoiding common pitfalls with modern microservices testing

Slide 2

Slide 2 text

@edeandrea #RedHat https://red.ht/quarkus-spring-devs • How Quarkus enabled modern Java development & the Kubernetes-native experience • Introduce familiar Spring concepts, constructs, & conventions and how they map to Quarkus • Emphasis on testing patterns & practices

Slide 3

Slide 3 text

@edeandrea #RedHat

Slide 4

Slide 4 text

@edeandrea #RedHat “Every time we touch one microservice, all the others break.”

Slide 5

Slide 5 text

@edeandrea #RedHat

Slide 6

Slide 6 text

@edeandrea #RedHat “My end-to-end test environment is only up the third weekend every month.”

Slide 7

Slide 7 text

#RedHat @edeandrea Disclaimer!

Slide 8

Slide 8 text

#RedHat @edeandrea Disclaimer! I am not a tester. (or even test expert)

Slide 9

Slide 9 text

#RedHat @edeandrea Disclaimer! I am not a tester. (or even test expert) That’s ok– testing is every developer’s job!

Slide 10

Slide 10 text

@edeandrea #RedHat Why is testing microservices so hard?

Slide 11

Slide 11 text

@edeandrea #RedHat Netflix microservice architecture

Slide 12

Slide 12 text

@edeandrea #RedHat Death star “That’s no moon. It’s our architecture.”

Slide 13

Slide 13 text

@edeandrea #RedHat Do microservices solve all problems?

Slide 14

Slide 14 text

@edeandrea #RedHat A microservice system: a portrait

Slide 15

Slide 15 text

No content

Slide 16

Slide 16 text

No content

Slide 17

Slide 17 text

@edeandrea #RedHat Netflix microservice architecture

Slide 18

Slide 18 text

@edeandrea #RedHat Netflix microservice architecture Testing with microservices is easy!

Slide 19

Slide 19 text

@edeandrea #RedHat Netflix microservice architecture Testing with microservices is easy! My services are so small! Everything is decoupled!

Slide 20

Slide 20 text

@edeandrea #RedHat Netflix microservice architecture Testing with microservices is easy! My services are so small! Everything is decoupled! But….. If you only test the points, And not the lines…. Does the thing actually work?

Slide 21

Slide 21 text

@edeandrea #RedHat distributed != decoupled

Slide 22

Slide 22 text

@edeandrea #RedHat Understand the coupling

Slide 23

Slide 23 text

#RedHat @edeandrea “It is all that is left unsaid upon which tragedies are built.” - Kreia, Star Wars Knights of the Old Republic II: The Sith Lords

Slide 24

Slide 24 text

#RedHat @edeandrea

Slide 25

Slide 25 text

#RedHat @edeandrea This is not the answer.

Slide 26

Slide 26 text

#RedHat @edeandrea “You lied to me. How many other lies have I been told by the Council? And how do you know that you even have the truth?” – Anakin Skywalker

Slide 27

Slide 27 text

#RedHat @edeandrea “Many of the truths that we cling to depend on our viewpoint.” — Obi-Wan Kenobi, Return of the Jedi

Slide 28

Slide 28 text

#RedHat @edeandrea “In my book, experience outranks everything.” — Captain Rex, The Clone Wars

Slide 29

Slide 29 text

@edeandrea #RedHat Ok, so how do we try these out?

Slide 30

Slide 30 text

#RedHat @edeandrea Get good at testing.

Slide 31

Slide 31 text

#RedHat @edeandrea Get good at testing. And automation.

Slide 32

Slide 32 text

No content

Slide 33

Slide 33 text

end-to-end tests high realism high value

Slide 34

Slide 34 text

end-to-end tests unit tests low effort high realism high value

Slide 35

Slide 35 text

end-to-end tests unit tests integration tests low effort high realism high value

Slide 36

Slide 36 text

end-to-end tests unit tests integration tests low effort high realism

Slide 37

Slide 37 text

end-to-end tests unit tests integration tests low effort high realism tests with application server test REST endpoints tests with a database

Slide 38

Slide 38 text

#RedHat @edeandrea their code our code Why mocks aren’t enough

Slide 39

Slide 39 text

#RedHat @edeandrea our mock our code Why mocks aren’t enough

Slide 40

Slide 40 text

#RedHat @edeandrea our mock our code ✔ tests Why mocks aren’t enough

Slide 41

Slide 41 text

#RedHat @edeandrea our mock our code Why mocks aren’t enough

Slide 42

Slide 42 text

#RedHat @edeandrea our mock our code ✔ tests ✘ reality our code their actual code Why mocks aren’t enough

Slide 43

Slide 43 text

@edeandrea #RedHat a mock stormtrooper

Slide 44

Slide 44 text

@edeandrea #RedHat a mock stormtrooper “Aren’t you a little short for a stormtrooper?” – Princess Leia

Slide 45

Slide 45 text

#RedHat @edeandrea https://www.flickr.com/photos/jdhancock/4811282106 Source: https://medium.com/jung-skywalker/star-wars-symbology-luke-han-and-how-to-face-the-abyss-e9fa966319b5

Slide 46

Slide 46 text

@edeandrea #RedHat Demo The limitations of unit tests https://github.com/edeandrea/pact-quarkus-wookie-carpet-demo

Slide 47

Slide 47 text

@edeandrea #RedHat “Will someone get this big walking carpet out of my way?” – Princess Leia Demo The limitations of unit tests https://github.com/edeandrea/pact-quarkus-wookie-carpet-demo

Slide 48

Slide 48 text

@edeandrea #RedHat “Will someone get this big walking carpet out of my way?” – Princess Leia Demo The limitations of unit tests https://github.com/edeandrea/pact-quarkus-wookie-carpet-demo

Slide 49

Slide 49 text

@edeandrea #RedHat

Slide 50

Slide 50 text

@edeandrea #RedHat A potential solution

Slide 51

Slide 51 text

@edeandrea #RedHat How to test a fire alarm?

Slide 52

Slide 52 text

@edeandrea #RedHat

Slide 53

Slide 53 text

@edeandrea #RedHat How NOT to test a fire alarm!

Slide 54

Slide 54 text

@edeandrea #RedHat

Slide 55

Slide 55 text

No content

Slide 56

Slide 56 text

Unit testing a fire alarm

Slide 57

Slide 57 text

@edeandrea #RedHat uh … is that enough?

Slide 58

Slide 58 text

@edeandrea #RedHat

Slide 59

Slide 59 text

@edeandrea #RedHat contract testing a fire alarm

Slide 60

Slide 60 text

Photo: https://www.westcoastfirepits.com/shop/death-star

Slide 61

Slide 61 text

Photo: https://www.westcoastfirepits.com/shop/death-star How to SAFELY burn a Death Star!

Slide 62

Slide 62 text

end-to-end tests unit tests integration tests low effort high realism high value

Slide 63

Slide 63 text

end-to-end tests unit tests integration tests low effort high realism high value contract tests

Slide 64

Slide 64 text

#RedHat @edeandrea our code their code A better mock

Slide 65

Slide 65 text

#RedHat @edeandrea our code their code contract test mock functional test A better mock

Slide 66

Slide 66 text

#RedHat @edeandrea ✔ our tests ✔ their tests ✔ reality our code their code contract test mock functional test A better mock

Slide 67

Slide 67 text

#RedHat @edeandrea our code their code A better mock

Slide 68

Slide 68 text

#RedHat @edeandrea our code their code contract test mock functional test A better mock

Slide 69

Slide 69 text

#RedHat @edeandrea ✔ our tests ✘ their tests ✘ reality our code their code contract test mock functional test A better mock

Slide 70

Slide 70 text

#RedHat @edeandrea A better mock our code their code

Slide 71

Slide 71 text

#RedHat @edeandrea A better mock our code their code contract test mock functional test

Slide 72

Slide 72 text

#RedHat @edeandrea ✘ our tests ✔ their tests ✘ reality A better mock our code their code contract test mock functional test

Slide 73

Slide 73 text

@edeandrea #RedHat Pact Contract Testing

Slide 74

Slide 74 text

@edeandrea #RedHat Demo

Slide 75

Slide 75 text

@edeandrea #RedHat “Help me, Pact! You’re my only hope!” – Princess Leia (almost) Demo

Slide 76

Slide 76 text

@edeandrea #RedHat “Uh, you’re telling me that consumers can break a provider’s tests?”

Slide 77

Slide 77 text

No content

Slide 78

Slide 78 text

#RedHat @edeandrea Consumer-driven contract testing

Slide 79

Slide 79 text

#RedHat @edeandrea consumer Consumer-driven contract testing

Slide 80

Slide 80 text

#RedHat @edeandrea contract consumer tests mock declare (Pact) Pact fi le Consumer-driven contract testing Spring Cloud Contract Pact

Slide 81

Slide 81 text

#RedHat @edeandrea contract consumer tests provider mock declare (Pact) contract tests verify Pact fi le Consumer-driven contract testing Spring Cloud Contract Pact Pact

Slide 82

Slide 82 text

#RedHat @edeandrea contract consumer tests provider mock declare (Pact) contract tests verify Pact fi le Consumer-driven contract testing ☑ rich semantic testing Spring Cloud Contract Pact Pact

Slide 83

Slide 83 text

#RedHat @edeandrea contract consumer tests provider mock declare (Pact) contract tests verify Pact fi le Consumer-driven contract testing ☑ rich semantic testing ☑ provider can ‘develop to the contract’ instead of developing speculatively (TDD) Spring Cloud Contract Pact Pact

Slide 84

Slide 84 text

#RedHat @edeandrea contract consumer tests provider mock declare (Pact) contract tests verify Pact fi le Consumer-driven contract testing ☑ rich semantic testing ☑ provider can ‘develop to the contract’ instead of developing speculatively (TDD) ⛔ existing spec assets cannot be re-purposed Spring Cloud Contract Pact Pact

Slide 85

Slide 85 text

#RedHat @edeandrea contract consumer tests provider mock declare (Pact) contract tests verify Pact fi le Consumer-driven contract testing ☑ rich semantic testing ☑ provider can ‘develop to the contract’ instead of developing speculatively (TDD) ⛔ existing spec assets cannot be re-purposed ⛔ provider needs to know who consumers are Spring Cloud Contract Pact Pact

Slide 86

Slide 86 text

#RedHat @edeandrea contract consumer tests provider mock declare (Pact) contract tests verify Pact fi le Consumer-driven contract testing ☑ rich semantic testing ☑ provider can ‘develop to the contract’ instead of developing speculatively (TDD) ⛔ existing spec assets cannot be re-purposed ⛔ provider needs to know who consumers are ⛔ consumers can break provider CI Spring Cloud Contract Pact Pact

Slide 87

Slide 87 text

#RedHat @edeandrea …but I want to just use Swagger/OpenAPI

Slide 88

Slide 88 text

@edeandrea #RedHat Is Pact always the best choice?

Slide 89

Slide 89 text

#RedHat @edeandrea Provider-driven contract testing

Slide 90

Slide 90 text

#RedHat @edeandrea consumer provider Provider-driven contract testing

Slide 91

Slide 91 text

#RedHat @edeandrea contract consumer provider OpenAPI Provider-driven contract testing generate (OpenAPI snapshot of current implementation) other protocol specs

Slide 92

Slide 92 text

#RedHat @edeandrea contract consumer provider contract tests verify OpenAPI Provider-driven contract testing generate (OpenAPI snapshot of current implementation) other protocol specs Schemathesis Microcks

Slide 93

Slide 93 text

#RedHat @edeandrea contract consumer tests provider mock contract tests verify OpenAPI Provider-driven contract testing generate (OpenAPI snapshot of current implementation) other protocol specs Prism Microcks Schemathesis Microcks

Slide 94

Slide 94 text

#RedHat @edeandrea 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 Prism Microcks Schemathesis Microcks

Slide 95

Slide 95 text

#RedHat @edeandrea 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 Prism Microcks Schemathesis Microcks

Slide 96

Slide 96 text

#RedHat @edeandrea 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 (deprecation is harder) Prism Microcks Schemathesis Microcks

Slide 97

Slide 97 text

#RedHat @edeandrea 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 (deprecation is harder) ⛔ spec-based tests lack functional depth Prism Microcks Schemathesis Microcks

Slide 98

Slide 98 text

#RedHat @edeandrea Is OpenAPI enough? It has the same syntax, but the semantics are different.

Slide 99

Slide 99 text

#RedHat @edeandrea Is OpenAPI enough? It has the same syntax, but the semantics are different. It looks the same, but the behaviour is different.

Slide 100

Slide 100 text

#RedHat @edeandrea Is OpenAPI enough? It has the same syntax, but the semantics are different. It looks the same, but the behaviour is different. Looks like a storm trooper. Behaves like Luke. (because it it is Luke)

Slide 101

Slide 101 text

@edeandrea #RedHat Demo: Semantic testing with Pact

Slide 102

Slide 102 text

#RedHat @edeandrea 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 ugh, 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….

Slide 103

Slide 103 text

@edeandrea #RedHat

Slide 104

Slide 104 text

@edeandrea #RedHat soo … microservices testing is hard limit end-to-end testing mocks will not be enough contract testing can rescue you

Slide 105

Slide 105 text

@edeandrea #RedHat Additional Resources

Slide 106

Slide 106 text

#RedHat @edeandrea https://github.com/quarkusio/quarkus-super-heroes More realistic sample application (fewer wookies)

Slide 107

Slide 107 text

#RedHat @edeandrea Kubernetes Native Java with Quarkus Deployment Developer Joy ● Quickly & easily build Kubernetes-Native Java applications ● Industry leader in startup time and memory utilization for native and JVM applications ● One step native executable generation ● Leverage existing Java, Jakarta, MicroProfile, & Spring expertise ● Imperative & reactive APIs side-by-side ● Live coding with zero config https://www.infoq.com/articles/native-java-quarkus

Slide 108

Slide 108 text

#RedHat @edeandrea https://www.oreilly.com/library/view/reactive-systems-in/9781492091714 https://developers.redhat.com/e-books/understanding-quarkus https://developers.redhat.com/e-books/practising-quarkus

Slide 109

Slide 109 text

@edeandrea #RedHat ® Thank you! @edeandrea