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

Fake it until you make it. WireMock, Testcontai...

Oleg Nenashev
January 27, 2025
8

Fake it until you make it. WireMock, Testcontainers, Quarkus and Gradle

estcontainers has become one of the most popular tools for software integration testing. If you can put your system-under-test into a container, Docker compose cluster or a pod, this is what you're likely to use. If your target isn't ready, not containerizable or just too heavy for testing as is, you can always use mock testing frameworks, e.g. WireMock or MockServer in the JVM ecosystem. Can you combine both approaches? Sure!

In this talk, we will classify integration testing and discuss application areas for WireMock and Testcontainers, and discuss which tool to choose for each use-case. And why not both? We will discuss putting WireMock into Testcontainers, and getting the best from their features thanks to the new WireMock modules for Testcontainers. Last but not least, we will see how to put WireMock in front of the Testcontainers or your real system to do deeper tests or even chaos engineering.

P.S: We will focus on the Java, Golang or Rust examples, but the same approaches can be used everywhere

Oleg Nenashev

January 27, 2025
Tweet

More Decks by Oleg Nenashev

Transcript

  1. Oleg Nenashev, Gradle Inc. @oleg_nenashev Fake it until you `make

    it` API Integration testing with Testcontainers and WireMock IET Summit, 27 January 2025
  2. Integration Testing 11 • Multiple technology stacks and toolchains •

    Many teams involved • Sacred knowledge • [Almost] always too late
  3. > whoami @oleg_nenashev oleg-nenashev Dr. Nenashev / Mr. Jenkins Community

    Builder, Gradle Developer Tools Hacker Independent Consultant #RussiansAgainstPutin #StandWithUkraine
  4. 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
  5. ◦ Build Scans and Troubleshooting ◦ Local and Distributed build

    caching ◦ Predictive Test Selection ◦ Flaky Tests Detection ◦ Local build observability ◦ // And more! https://gradle.com/develocity/
  6. 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
  7. +

  8. 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
  9. 29 Developer Productivity Engineering (DPE) • Culture • Metrics &

    Observability • Developer Tools • Developer Services
  10. 32 Staging & QA Tests Build Integration tests Build Tests

    Gaining confidence in you software “Shift Left” with modern tools
  11. 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
  12. 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
  13. 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
  14. 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
  15. 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
  16. WireMock Ecosystem / Java • Java • Kotlin • Scala

    • Groovy • Clojure • (?)Android 52 • JUnit 4/5 • Testcontainers • Sprint Boot • Spring Cloud Contract • Quarkus • Maven/Gradle • Spock • Pact
  17. WireMock as a Proxy Tests API Provider • Fault injection

    • Recording • Protocol Verification
  18. 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 59 * Your mileage may vary ** More later *** I just like footnotes * Likely, you want to use both
  19. 60

  20. 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 63
  21. 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 65 TL;DR: Two Olegs
  22. Quarkus Dev Services • Automatic provisioning of unconfigured services in

    development and test mode • Transparent to your application • Mostly containerized, with Testcontainers 67 https://quarkus.io/guides/dev-services
  23. Dev Services • Mostly containerized, with Testcontainers 68 Presentation by

    Daniel Oh: https://www.youtube.com/watch?v=moIFTj9KW1c
  24. 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
  25. Takeaways 80 • 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!
  26. 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 81 * Your mileage may vary ** More later *** I just like footnotes * Likely, you want to use both
  27. References • This presentation: speakerdeck.com/onenashev/ • Gradle: https://gradle.com • Gradle

    Build Tool: https://gradle.org • Gradle Build Scan: scans.gradle.com
  28. Credits 87 • All WireMock, Testcontainers and Quarkus contributors •

    Siva Prasad Reddy, JetBrains and Oleg Shelajev, Docker • All FOSS folks