Skip to content
SimplyMe
Go back

Taming External Dependencies in Quarkus Tests: Ditch Testcontainers for Unit Tests

Edit page

When you embrace Quarkus for its supersonic subatomic Java experience, testing is paramount. However, the default behavior of @QuarkusTest can sometimes pull in more than you bargained for, especially when dealing with external dependencies like databases or Redis. Suddenly, your “unit” tests are spinning up Docker containers, slowing down your feedback loop. Fear not, fellow Quarkus enthusiasts! Let’s explore how to effectively disable these external services and keep our unit tests truly isolated.

The Challenge: Unwanted Testcontainers

By default, Quarkus, when encountering configurations for databases or Redis, tends to fire up Testcontainers within @QuarkusTest. While fantastic for integration tests, this overhead is undesirable for quick, focused unit tests. We want to mock, not provision.

The Solution: Strategic Disabling

Here’s a breakdown of the strategies you can employ to reign in those external dependencies:

1. The Power of @TestProfile and Conditional Configuration

This is arguably the most robust and recommended approach.

2. Embrace Mocking with @InjectMock

This is the most important part of unit testing.

3. Conditional Beans (Less Common)

Key Takeaways

By employing these strategies, you can effectively decouple your Quarkus unit tests from external services, leading to faster, more reliable, and more maintainable tests. Happy testing!


Edit page
Share this post on:

Previous Post
Level Up Your Tech Leadership: Choosing the Right Personal Knowledge Management (PKM) Method
Next Post
Beyond Pandas: Exploring Big Data with Polars, Dask, and Vaex in VS Code