Slide 1

Slide 1 text

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

Slide 2

Slide 2 text

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

Slide 3

Slide 3 text

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

Slide 4

Slide 4 text

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

Slide 5

Slide 5 text

About You 6

Slide 6

Slide 6 text

When IT Goes wrong 7

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 10

Slide 10

Slide 10 text

No content

Slide 11

Slide 11 text

Integration Testing 12 ● 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

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

Slide 15

Slide 15 text

API specs API tech

Slide 16

Slide 16 text

No content

Slide 17

Slide 17 text

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

Slide 18

Slide 18 text

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

Slide 19

Slide 19 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 20

Slide 20 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 21

Slide 21 text

No content

Slide 22

Slide 22 text

Example - Java 23

Slide 23

Slide 23 text

https://testcontainers.com/modules/

Slide 24

Slide 24 text

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

Slide 25

Slide 25 text

26 SDKs testcontainers.org … and

Slide 26

Slide 26 text

27 github.com/testcontainers/ testcontainers-go

Slide 27

Slide 27 text

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

Slide 28

Slide 28 text

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

Slide 29

Slide 29 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 30

Slide 30 text

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

Slide 31

Slide 31 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 32

Slide 32 text

No content

Slide 33

Slide 33 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 34

Slide 34 text

WireMock Ecosystem & Features wiremock.org/docs

Slide 35

Slide 35 text

WireMock Ecosystem wiremock.org/docs

Slide 36

Slide 36 text

WireMock in Java wiremock.org/docs/stubbing * Gradle® and the Gradlephant logo are registered trademarks of Gradle, Inc.

Slide 37

Slide 37 text

WireMock Ecosystem / Java ● Java ● Kotlin ● Scala ● Groovy ● Clojure ● (?)Android 38 ● JUnit 4/5 ● Testcontainers ● Sprint Boot ● Spring Cloud Contract ● Quarkus ● Maven / Gradle Build Tool ● Spock ● Pact * Gradle® and the Gradlephant logo are registered trademarks of Gradle, Inc. "Gradle" means "Gradle Build Tool" and does not reference Gradle, Inc. and/or its subsidiaries

Slide 38

Slide 38 text

Example - JUnit 5 39

Slide 39

Slide 39 text

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

Slide 40

Slide 40 text

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

Slide 41

Slide 41 text

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

Slide 42

Slide 42 text

WireMock or Testcontainers?

Slide 43

Slide 43 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 44 * Your mileage may vary ** More later *** I just like footnotes * Likely, you want to use both

Slide 44

Slide 44 text

45

Slide 45

Slide 45 text

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

Slide 46

Slide 46 text

Proxy can be made transparent in your test! 47 Tests

Slide 47

Slide 47 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 48

Slide 48

Slide 48 text

Quarkus or Spring What do you use ?

Slide 49

Slide 49 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 50 TL;DR: Two Olegs

Slide 50

Slide 50 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 51

Slide 51 text

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

Slide 52

Slide 52 text

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

Slide 53

Slide 53 text

No content

Slide 54

Slide 54 text

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

Slide 55

Slide 55 text

@QuarkusTest or @QuarkusIntegrationTest

Slide 56

Slide 56 text

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

Slide 57

Slide 57 text

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

Slide 58

Slide 58 text

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

Slide 59

Slide 59 text

No content

Slide 60

Slide 60 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: 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 61

Slide 61 text

Lessons Learned 62

Slide 62

Slide 62 text

Takeaways 63 ● 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 63

Slide 63 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 64 * Your mileage may vary ** More later *** I just like footnotes * Likely, you want to use both

Slide 64

Slide 64 text

Keep Learning Tools 65 * AKA: Burning Out 101

Slide 65

Slide 65 text

No content

Slide 66

Slide 66 text

It’s [always] a great time to contribute! testcontainers.org/contributing wiremock.org/participate 67

Slide 67

Slide 67 text

GitHub: oleg-nenashev Twitter: @oleg_nenashev WireMock Slack Questions? Testcontainers Slack 68

Slide 68

Slide 68 text

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