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

API Testing & Mocking using Testcontainers

API Testing & Mocking using Testcontainers

This deck explores how to use Testcontainers and Microcks to simplify API testing and mocking in distributed systems. Learn how Testcontainers provides reliable, throwaway container instances for tests, and how Microcks can instantly create live API mocks from your specifications. Discover how this powerful combination solves common development challenges and simplifies your testing workflow.

Avatar for Hugo Guerrero

Hugo Guerrero

September 11, 2025
Tweet

More Decks by Hugo Guerrero

Other Decks in Technology

Transcript

  1. © Kong Inc. © Kong Inc. Repo we will use

    today https://github.com/microcks/microcks-quarkus-demo 3
  2. © Kong Inc. 7 The pain of the inner dev/test

    loop • Inner dev/test loop can be painful with Microservices and Kubernetes • Pain increases as the number of services in your system increases Integration Testing
  3. © Kong Inc. 8 It's not that easy to get

    it right Usually means “integration testing environment”: • Shared Instances • Local Installation • In memory solutions • Docker compose Everything is up and running before running the test. Integration Testing https://www.youtube.com/watch?v=3tP31j_T6IE
  4. © Kong Inc. 9 Shifting Left µS A Redis 🔌

    REST API A 🔌 REST API C µS B 🔌 REST API B 🔌 gRPC svc D Diversity of stacks, of middleware, and of APIs make things difficult to set up, test and validate during development on the developer laptop (the inner loop)
  5. © Kong Inc. 10 Inner Loop—the actions of the developer:

    code, build, debug, push, repeat Outer Loop—the actions of the Ops Persona: review, build, check, test, deploy, triggered by the push action of the Inner Loop
  6. © Kong Inc. Introducing Testcontainers https://github.com/testcontainers 11 • Creates a

    reliable environment for running local workloads. • It's a library, ported to multiple languages • Containerised common test dependencies • Ability to run custom Containerfiles when needed. • Flexible enough to manage the container lifecycle and advanced networking
  7. © Kong Inc. 12 During test execution, your code interacts

    with the live container, ensuring a realistic and isolated environment for your integration tests. Before your tests run, Testcontainers provisions a fresh container instance with the specified dependencies. After the tests are complete, Testcontainers automatically tears down the container, leaving your environment clean and preventing resource leaks.
  8. © Kong Inc. 13 AtomicJar partners with software vendors to

    maintain and certify official modules. Java (Spring Boot & Quarkus), NodeJS, Go
  9. © Kong Inc. https://github.com/testcontainers/testcontainers-java Testcontainers-Java: A Closer Look 14 •

    Created 9 years ago • Uses docker-java API • Integrates with Frameworks, like JUnit • Works with anything that runs in a container • Java library for Testcontainers that enables embedding API mocking and testing into your JUnit tests with lightweight, throwaway instance thanks to containers
  10. © Kong Inc. Quarkus Dev Services Quarkus Extensions 15 Quarkus

    extension that enables embedding API Mocking and Testing as a DevService managing mocks for dependencies and contract-testing your API endpoints.
  11. © Kong Inc. TestContainers Covers ALL Kinds of APIs 16

    OpenAPI Specification 2.x, 3.x AsyncAPI Specification 2.x Traditional APIs Event-driven Systems CloudEvents Specification 1.0.x Soap WebServices (SoapUI) 5.1+ Postman Collection (JSON) 2.x GraphQL gRPC Services 3.x General Purpose Kafka broker MQTT broker WebSocket server RabbitMQ NATS.io Google Pub/Sub AWS SQS + SNS
  12. © Kong Inc. Multi-Artifact Support 17 OpenAPI (Primary) Import Import

    API / Service metadata operations examples examples Test suite Others (Secondary)
  13. © Kong Inc. Come for the performance, stay for the

    productivity Developer Productivity 18 Dev UI Visualize and configure extensions as well as access to application logs and testing components Dev Services Automatic provisioning and application wiring of supporting services such as databases, identity servers, and more. Continuous Testing Get instant feedback on code changes as tests run in the background on impacted code. CLI Remote Dev Run dev mode remotely with changes to local files immediately available in a containerized environment Create projects, manage extensions, and execute essential build and dev commands.
  14. © Kong Inc. Our pastry shop 21 Let’s imagine …

    🍰 🎂 🧁 🍩 Product Domain Pastry API Order Domain Order Service Stores Partners Online Retrieve Place orders
  15. © Kong Inc. A New Player: Microcks Introducing Microcks 22

    • Microcks is a Cloud Native Computing Foundation (CNCF) Sandbox project. • It's a platform for API mocking and contract testing directly from your API specifications. • It turns your API assets (like OpenAPI, AsyncAPI, etc.) into live mocks in seconds. • It also reuses these assets to run compliance and non-regression tests.
  16. © Kong Inc. The Role of Microcks in Our Example

    Microcks in Our Pastry Shop 23 • API Mocking: Microcks will provide a live, mock version of the Pastry API to our Order Service. This allows us to test the Order Service without needing a real Pastry API running. • Contract Testing: We will use Microcks to verify that our Order Service is correctly implementing the contract for its own API (the one used by Online, Stores, and Partners).
  17. © Kong Inc. A New Player: Microcks Microcks with Testcontainers

    24 • The Quarkus DevServices feature automatically provisions a Microcks instance inside a Testcontainers container. • This happens when you run your application in dev mode or during tests. • No manual setup is required—just the right dependencies and your API artifacts.
  18. © Kong Inc. The Pastry Shop API Contract The Pastry

    Shop Contract 25 • openapi.yml is the "single source of truth." • It defines the API's structure: ◦ GET /pastry ◦ GET /pastry/{name} ◦ PATCH /pastry/{name} • It also includes example data to be used for mocking.
  19. © Kong Inc. Conclusion of Demo Demo Recap 27 •

    We've used Testcontainers to automatically spin up a Microcks instance. • Microcks read our API contract and provided a functional mock. • We can now test our Order Service against a realistic, mocked Pastry API without any external dependencies.
  20. © Kong Inc. Contract Testing 28 RANDOM EXAMPLES RANDOM REAL-LIFE

    BUSINESS EXPECTATIONS EXECUTABLE SPECIFICATIONS MOCKS & CONFORMANCE TESTS Contracts Examples
  21. © Kong Inc. Contract Testing Levels 29 Contract Testing Syntactic

    conformance Behavior conformance The provider actually respects the structure of messages. Consumers will be technically able to interact with the API. The provider actually behaves consistently regarding the business domain. Consumers will be able to use and build upon it.
  22. © Kong Inc. Key Takeaways 30 API CONTRACT TESTING is

    essential for distributed applications Testcontainers offers a flexible model for different testing strategies Testcontainers solves scaling challenges with a uniform approach 1 2 3
  23. © Kong Inc. Try Microcks (microcks.io)! Follow us on Twitter

    @microcksio, Join the community - Discord: microcks.io/discord-invite/ Distribute & if you like it. It’s a 100% Open Source project. And if you love it, contribute! We need you! 31 Calling the community
  24. © Kong Inc. Thank you! Konghq.com Kong Inc. [email protected] Interested

    in learning more? Don’t hesitate to reach out at the booth!