Hi everyone,
recently I came across an issue [1] with a long discussion about
@QuarkusTest being a little bit slow in some cases. Especially when you
run a single test from the IDE and your app has a lot of extensions and
possibly some dev services.
So here comes a working POC called "QuarkusComponentTest".
What is it for?
- The goal is to test business logic in one or more components/CDI beans
without the need to wire and mock all dependencies manually.
How is it implemented?
- It's a regular JUnit5 extension; a static field of a test class
annotated with @RegisterExtension.
What does it do?
- Starts the CDI container (ArC)
- Registers a dedicated SmallRyeConfig
- Unsatisfied dependencies are mocked automatically; you can inject them
in the test and configure the mocks right away
- There is also a builder-like API to provide custom mock beans
- The CDI request context is activated and terminated per test method
What's missing?
- Advanced SmallRye Config integration
- QuarkusComponentTests are currently skipped during continuous testing;
we would need some class loading magic to make it work
- Further integration with extensions, e.g. an extension could register
a customized mock automatically?
Examples? Code?
- There is a quickstarts branch with examples:
https://github.com/mkouba/quarkus-quickstarts/blob/component-test/config-quickstart/src/test/java/org/acme/config/MyComponentV1Test.java
- The POC branch is available here:
https://github.com/mkouba/quarkus/tree/component-test
The question is whether this prototype is worth further investigation
and possibly integrating into the Quarkus core testing tools?
--
Martin Kouba
Principal Software Engineer
Red Hat, Czech Republic
[1]
https://github.com/quarkusio/quarkus/issues/14927