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

Wokrskshop: API Integration testing for Spring...

Avatar for Oleg Nenashev Oleg Nenashev PRO
May 22, 2025
11

Wokrskshop: API Integration testing for Spring Boot apps, with Testcontainers and WireMock

Testcontainers have 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 pod, you’re likely to use this. 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 of Spring projects 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.

Bonus part from Oleg Šelajev: Let's add some AI? Using Spring AI and SLMs for test purposes

P.S: We will focus on the Java/Kotlin examples and Spring, but the same approaches can be used everywhere

Avatar for Oleg Nenashev

Oleg Nenashev PRO

May 22, 2025

More Decks by Oleg Nenashev

Transcript

  1. WORKSHOP: API Integration testing for Spring apps, with Testcontainers and

    WireMock Oleg Nenashev, Gradle Oleg Šelajev, Docker
  2. Demo & Workshop repository • github.com/oleg-nenashev/ spring_io_2025-Testcontainers-workshop • Target app:

    Tracking the TODO list and listing some news for your morning coffee! NOTE: Trademarked names, project and company logos belong to their respective owners 3
  3. We will use Gradle Build Tool for the demo this

    time, Oleg TL;DR: Two Olegs 5
  4. Oleg #1 Dr. Nenashev / Mr. Jenkins Community Builder, Gradle

    Developer Tools Hacker @oleg_nenashev oleg-nenashev #RussiansAgainstPutin #StandWithUkraine
  5. Oleg #2 • • • • Surprisingly, a Java Champion

    18+ years software development experience 11 years Developer Advocate Loves to stare at the code of Open Source projects Quarkus Spring Boot LangChain4j Microcks Testcontainers (sometimes contributes bugs too!) linkedin.com/in/shelajev/ youtube.com/user/shelajev/shorts
  6. What we talk about today? 1. Spring Dev Services 2.

    Testcontainers 3. WireMock 4. Everything together 5. Local models for modeling / testing AI 6. Integrations 8
  7. Today’s lab The workshop! On-demands: • API Mocking 101 •

    Integration Testing 101 • Testcontainers 101 • WireMock 101 • Testing AI applications 15
  8. Before We Start • Level of experience with tech we

    use • Do you use Testcontainers? • Do you use WireMock or Microcks? • Do you already use Spring Boot Dev Services? 16
  9. Want to follow along? • A Laptop (?) • Recommended:

    MacOS / Windows laptops • Your Favorite IDE • Docker Desktop 17
  10. Options • Manual Docker Compose & Co • Dev Services

    with Docker Compose • Dev Service with Testcontainers (!) ◦ Test app with ./gradlew bootTestRun https://docs.spring.io/spring-boot/reference/fe atures/dev-services.html 22
  11. Integration Testing • Multiple technology stacks and toolchains • Many

    teams involved • Sacred knowledge • [Almost] always too late 25
  12. APIs are the Key for Developer Productivity Client App request

    request response response HTTP/2 API API Server Storage
  13. Gaining confidence in you software Build Tests “Shift Left” with

    modern tools Integration tests Build Staging & QA Tests 32
  14. Ways to do API integration testing 1. Testing against Production/Staging

    servers Accurate Slow Nope Fast 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 * For Testcontainers, see the slides
  15. Ways to do API integration testing 1. Testing against Production/Staging

    servers Accurate Slow Nope Fast 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 * For Testcontainers, see the slides
  16. Ways to do API integration testing 1. Testing against Production/Staging

    servers Accurate Slow Nope Fast 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 * For Testcontainers, see the slides
  17. API Mocking Tools Main implementation in Java Sandbox CNCF Project

    Native lib, Docker images and Testcontainers modules Golang / Microservice arch Support for GraalVM Docker image, Testcontainers module, All-in K8s Charts Rich Ecosystem Less Rich Ecosystem
  18. API Mocking 101 WireMock Config JSON: IF (request_url) THEN (response)

    * Client library request response HTTP/2 Client App Mock API Server * it gets MUCH more complex wiremock.org/docs/stubbing
  19. WireMock Ecosystem / Java • Java • Kotlin • Scala

    • Groovy • Clojure • (?)Android • • • • • • • • JUnit 4/5 Testcontainers Sprint Boot Spring Cloud Contract Quarkus Maven / Gradle Build Tool Spock Pact 53
  20. WireMock for Spring Boot • There is a library for

    it! • https://wiremock.org/docs/solutions/ spring-boot-integration/ 56
  21. 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 * Likely, you want to use both * Your mileage may vary ** More later *** I just like footnotes 60
  22. 61

  23. 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
  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: 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. Docker Model Runner ➔ Run models locally with full GPU

    support ➔ Distribute models through container registries ➔ Easily integrate using any OpenAI-compatible SDKs and libraries compose.yaml services: llm: provider: type: model options: model: ai/gemma3:4B-F16 app:
  26. Resources • Docker Model Runner docs: https://docs.docker.com/model-runner/ • Using Docker

    Model in Docker Compose https://docs.docker.com/compose/how-tos/model-runner/#provider-services • Using Docker Model Runner for AI assistant setup: https://dev.to/olegshelajev/easy-private-ai-assistant-with-goose-and-docker-model-runner-4b41
  27. Takeaways • 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! 70
  28. 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 * Likely, you want to use both * Your mileage may vary ** More later *** I just like footnotes 71