Slide 1

Slide 1 text

Oleg Nenashev, Gradle Inc. @oleg_nenashev Fake it until you `make it` API Integration testing with Testcontainers and WireMock IET Summit, 27 January 2025

Slide 2

Slide 2 text

Async Q&A: Gradle Community Slack, Testcontainers and WireMock Slack speakerdeck.com/onenashev

Slide 3

Slide 3 text

About You 3

Slide 4

Slide 4 text

https://jobs.mobiliar.ch/Vacancies/4624/Description/1

Slide 5

Slide 5 text

You Have a LOT to Integrate 5 https://jobs.mobiliar.ch/Vacancies/4624/Description/1

Slide 6

Slide 6 text

When IT Goes wrong 6

Slide 7

Slide 7 text

No content

Slide 8

Slide 8 text

No content

Slide 9

Slide 9 text

Do you `make it`? IT - Integration Tests 9

Slide 10

Slide 10 text

No content

Slide 11

Slide 11 text

Integration Testing 11 ● Multiple technology stacks and toolchains ● Many teams involved ● Sacred knowledge ● [Almost] always too late

Slide 12

Slide 12 text

No content

Slide 13

Slide 13 text

No content

Slide 14

Slide 14 text

> whoami @oleg_nenashev oleg-nenashev Dr. Nenashev / Mr. Jenkins Community Builder, Gradle Developer Tools Hacker Independent Consultant #RussiansAgainstPutin #StandWithUkraine

Slide 15

Slide 15 text

rts.ch/info/regions/neuchatel/12836885-la-population-neuchateloise-en-hausse-malgre-un-recul-dans-les-montagnes.html

Slide 16

Slide 16 text

* Saw (2004) Yes, that elephant in the room

Slide 17

Slide 17 text

17 Developer Productivity Engineering (DPE)

Slide 18

Slide 18 text

No content

Slide 19

Slide 19 text

About You 19 Maven or Gradle ?

Slide 20

Slide 20 text

Disclaimer Gradle == Gradle Build Tool in this talk It’s not a “Gradle vs Maven” talk. It is not a Gradle one I use both Maven and Gradle IMO: There is no place for “vs” in open source

Slide 21

Slide 21 text

2 1 And we mean it Valentine Day’s post

Slide 22

Slide 22 text

22 https://gradle.com/develocity/

Slide 23

Slide 23 text

○ Build Scans and Troubleshooting ○ Local and Distributed build caching ○ Predictive Test Selection ○ Flaky Tests Detection ○ Local build observability ○ // And more! https://gradle.com/develocity/

Slide 24

Slide 24 text

For many FOSS projects: we sponsor Develocity licenses/hosting API for integration and developer pipelines Maven, Bazel and sbt users welcome! gradle/develocity-oss-projects gradle/develocity-oss-projects

Slide 25

Slide 25 text

+

Slide 26

Slide 26 text

See ge.apache.org helps Used in Beam, Groovy, Kafka, Lucent, Pulsar, Solr, etc. ● 37 onboarded projects ● 31 days per week - savings from the build cache ● 116 days per week - savings from all caching ● 30k builds per week ● 503d of total build time per week

Slide 27

Slide 27 text

DPE is Like SRE, but for Developer Productivity 27

Slide 28

Slide 28 text

28 Developer Productivity Engineering (DPE) ● Culture ● Metrics & Observability ● Developer Tools ● Developer Services

Slide 29

Slide 29 text

No content

Slide 30

Slide 30 text

APIs are the Key for Developer Productivity response request Client App Server HTTP/2 Storage response request API API

Slide 31

Slide 31 text

API specs API tech

Slide 32

Slide 32 text

No content

Slide 33

Slide 33 text

33 Staging & QA Tests Build Integration tests Build Tests Gaining confidence in you software “Shift Left” with modern tools

Slide 34

Slide 34 text

34 https://martinfowler.com/articles/ mocksArentStubs.html

Slide 35

Slide 35 text

Ways to do API integration testing 1. Testing against Production/Staging servers 2. Testing against a simplified/containerized instance (e.g. Testcontainers) 3. Modeling (aka Mocking) at the API provider level (e.g. WireMock) 4. Mocking at the code level Slow Fast Nope Accu- rate * For Testcontainers, see the slides

Slide 36

Slide 36 text

Ways to do API integration testing 1. Testing against Production/Staging servers 2. Testing against a simplified/containerized instance (e.g. Testcontainers) 3. Modeling (aka Mocking) at the API provider level (e.g. WireMock or Microcks) 4. Mocking at the code level Slow Fast Nope Accu- rate * For Testcontainers, see the slides

Slide 37

Slide 37 text

No content

Slide 38

Slide 38 text

Example - Java 38

Slide 39

Slide 39 text

https://testcontainers.com/modules/

Slide 40

Slide 40 text

40 LOCAL STACK https://testcontainers.com/modules/

Slide 41

Slide 41 text

41 SDKs testcontainers.org … and

Slide 42

Slide 42 text

42 github.com/testcontainers/ testcontainers-go

Slide 43

Slide 43 text

Demo Time 43 github.com/oleg-nenashev/mocks-as-code-demo/ tree/main/1_application

Slide 44

Slide 44 text

No Implementation? 44 Fake it until you “$ make it”

Slide 45

Slide 45 text

Ways to do API integration testing 1. Testing against Production/Staging servers 2. Testing against a simplified/containerized instance (e.g. Testcontainers) 3. Modeling (aka Mocking) at the API provider level (e.g. WireMock or Microcks) 4. Mocking at the code level Slow Fast Nope Accu- rate * For Testcontainers, see the slides

Slide 46

Slide 46 text

API Mocking for Java. Examples en.wikipedia.org/wiki/Comparison_of_API_simulation_tools Proprietary Open Source `

Slide 47

Slide 47 text

API Mocking Tools Main implementation in Java Native lib, Docker images and Testcontainers modules Support for GraalVM Rich Ecosystem Sandbox CNCF Project Golang / Microservice arch Docker image, Testcontainers module, All-in K8s Charts Less Rich Ecosystem

Slide 48

Slide 48 text

No content

Slide 49

Slide 49 text

wiremock.org/docs/stubbing IF (request_url) THEN (response) * * it gets MUCH more complex WireMock Config JSON: Client library response request Client App Mock API Server HTTP/2 API Mocking 101

Slide 50

Slide 50 text

WireMock Ecosystem & Features wiremock.org/docs

Slide 51

Slide 51 text

WireMock Ecosystem wiremock.org/docs

Slide 52

Slide 52 text

WireMock in Java wiremock.org/docs/stubbing

Slide 53

Slide 53 text

WireMock Ecosystem / Java ● Java ● Kotlin ● Scala ● Groovy ● Clojure ● (?)Android 53 ● JUnit 4/5 ● Testcontainers ● Sprint Boot ● Spring Cloud Contract ● Quarkus ● Maven/Gradle ● Spock ● Pact

Slide 54

Slide 54 text

Example - JUnit 5 54

Slide 55

Slide 55 text

WireMock Plugin for Gradle 55 William-Hill-Online/wiremock-gradle-plugin

Slide 56

Slide 56 text

56 WireMock has an official Testcontainers module! testcontainers.com/modules/wiremock

Slide 57

Slide 57 text

WireMock as a Proxy Tests API Provider ● Fault injection ● Recording ● Protocol Verification

Slide 58

Slide 58 text

58 DEMO TIME https://github.com/wiremock/wiremock-docker

Slide 59

Slide 59 text

WireMock or Testcontainers?

Slide 60

Slide 60 text

Choosing a right tool * ● Need database or complex dynamic environment? ○ YES => Testcontainers ● Do you have a ready implementation? ○ NO => WireMock/Microcks ● Is it containerized and lightweight? ○ NO => WireMock/Microcks ● Can you easily trigger the desired scenarios? ○ NO => WireMock/Microcks ● Else: => Testcontainers 60 * Your mileage may vary ** More later *** I just like footnotes * Likely, you want to use both

Slide 61

Slide 61 text

61

Slide 62

Slide 62 text

62 WireMock has an official Testcontainers module! testcontainers.com/modules/wiremock

Slide 63

Slide 63 text

Proxy can be made transparent in your test! 63 Tests

Slide 64

Slide 64 text

WireMock Proxy? Why? ● Record & Playback for Request sequences ● Reverse engineering of legacy undocumented APIs ● NEW: OpenAPI Standard Compliance Verification (in 3.0 Beta) ● Emulating complex cases ○ Overriding particular responses ○ Emulating response sequences / Stateful behavior ○ Failure Injection 64

Slide 65

Slide 65 text

Quarkus or Spring What do you use ?

Slide 66

Slide 66 text

If you like / use You can do [almost] the same as below Devoxx BE Video with Oleg Shelajev: youtube.com/watch?v=eFILbyaMI2A Our demo: github.com/testcontainers-community/ testcontainers-wiremock-demo 66 TL;DR: Two Olegs

Slide 67

Slide 67 text

Source: https://www.codelikethewind.org/2022/05/03/3-reasons- why-all-java-developers-should-consider-quarkus/ ● Reference implementation of new Java Developer Experience ● Spring Boot is catching up ● Micronaut & Co are great, too

Slide 68

Slide 68 text

Quarkus Dev Services ● Automatic provisioning of unconfigured services in development and test mode ● Transparent to your application ● Mostly containerized, with Testcontainers 68 https://quarkus.io/guides/dev-services

Slide 69

Slide 69 text

Dev Services ● Mostly containerized, with Testcontainers 69 Presentation by Daniel Oh: https://www.youtube.com/watch?v=moIFTj9KW1c

Slide 70

Slide 70 text

No content

Slide 71

Slide 71 text

71 docs.quarkiverse.io/quarkus-wiremock/dev/index.html

Slide 72

Slide 72 text

@QuarkusTest or @QuarkusIntegrationTest

Slide 73

Slide 73 text

And a Quarkus Dev Service! docs.quarkiverse.io/quarkus-wiremock/dev/index.html

Slide 74

Slide 74 text

docs.quarkiverse.io/qu arkus-wiremock/dev/in dex.html#extension-co nfiguration-reference

Slide 75

Slide 75 text

75 DEMO TIME https://github.com/oleg-nenashev/gradle-quarkus-plugin-demo

Slide 76

Slide 76 text

No content

Slide 77

Slide 77 text

No content

Slide 78

Slide 78 text

Gradle Build Scan & Develocity 78

Slide 79

Slide 79 text

More Demos ● Simple WireMock and Testcontainers demo: github.com/testcontainers-community/testcontainers-wiremock-demo ● WireMock and Docker integration tests: https://github.com/wiremock/wiremock-docker ● Quarkus Dev Service for WireMock and Testcontainers, in Gradle: https://github.com/oleg-nenashev/gradle-quarkus-plugin-demo ● Spring Boot + Testcontainers and WireMock + LocalStack: https://github.com/oleg-nenashev/mocks-as-code-demo

Slide 80

Slide 80 text

Lessons Learned 80

Slide 81

Slide 81 text

Takeaways 81 ● Shift left the integration testing ● API Mocking / abstraction are great… when used right ● There is no silver bullet ● WireMock and Testcontainers address their own use-cases ● They work well together!

Slide 82

Slide 82 text

Choosing a right tool * ● Need database or complex dynamic environment? ○ YES => Testcontainers ● Do you have a ready implementation? ○ NO => WireMock/Microcks ● Is it containerized and lightweight? ○ NO => WireMock/Microcks ● Can you easily trigger the desired scenarios? ○ NO => WireMock/Microcks ● Else: => Testcontainers 82 * Your mileage may vary ** More later *** I just like footnotes * Likely, you want to use both

Slide 83

Slide 83 text

Keep Learning Tools 83 * AKA: Burning Out 101

Slide 84

Slide 84 text

No content

Slide 85

Slide 85 text

It’s [always] a great time to contribute! gradle.github.io/community/contributing testcontainers.org/contributing wiremock.org/participate 85

Slide 86

Slide 86 text

GitHub: oleg-nenashev Twitter: @oleg_nenashev Gradle Community Slack, #developer-productivity-engineering Questions? Testcontainers Slack 86

Slide 87

Slide 87 text

References ● This presentation: speakerdeck.com/onenashev/ ● Gradle: https://gradle.com ● Gradle Build Tool: https://gradle.org ● Gradle Build Scan: scans.gradle.com

Slide 88

Slide 88 text

Credits 88 ● All WireMock, Testcontainers and Quarkus contributors ● Siva Prasad Reddy, JetBrains and Oleg Shelajev, Docker ● All FOSS folks