In my project based on Quarkus and reactive Panache (HibernateReactive) on the service layer there are business methods annotated with @WithTransaction
or @WithSession
.
They usually combine several calls to the underlying persistence layer (repositories).
I'd like to write unit test of the business logic which mocks all the real DB access.
I'm however unable to call the tested service method annotated with @WithTransaction
even thou it does not query/change any DB data.
The usual complaints are:
- there is no vertx context or you cannot block the vertx event loop thread
I've tried to mark the the method with
@ActivateRequestContext
and@RunOnVertxContext
. No success.
The only feasible way is to rewrite the test using @TestReactiveTransaction
.
This has one substantial drawback however - it requires active DB connection to call ROLLBACK at the end of the method execution.
So the question:
Is there a way to tell the Quarkus to ignore the @WithTransaction
when called from the unit test ??
In my project based on Quarkus and reactive Panache (HibernateReactive) on the service layer there are business methods annotated with @WithTransaction
or @WithSession
.
They usually combine several calls to the underlying persistence layer (repositories).
I'd like to write unit test of the business logic which mocks all the real DB access.
I'm however unable to call the tested service method annotated with @WithTransaction
even thou it does not query/change any DB data.
The usual complaints are:
- there is no vertx context or you cannot block the vertx event loop thread
I've tried to mark the the method with
@ActivateRequestContext
and@RunOnVertxContext
. No success.
The only feasible way is to rewrite the test using @TestReactiveTransaction
.
This has one substantial drawback however - it requires active DB connection to call ROLLBACK at the end of the method execution.
So the question:
Is there a way to tell the Quarkus to ignore the @WithTransaction
when called from the unit test ??
1 Answer
Reset to default 2We have examples of what you are trying to do in the Quarkus Superheroes Sample application: https://github/quarkusio/quarkus-super-heroes
Specifically this service layer: https://github/quarkusio/quarkus-super-heroes/blob/main/rest-heroes/src/main/java/io/quarkus/sample/superheroes/hero/service/HeroService.java
And these tests: https://github/quarkusio/quarkus-super-heroes/blob/main/rest-heroes/src/test/java/io/quarkus/sample/superheroes/hero/service/HeroServiceTests.java
I think they key is https://github/quarkusio/quarkus-super-heroes/blob/main/rest-heroes/src/test/java/io/quarkus/sample/superheroes/hero/service/HeroServiceTests.java - Injecting a mock of your repository (or using PanacheMock
if you are using the active record pattern).
If you are using the "real" database in your tests then yes you will need @TestReactiveTransaction
, like in https://github/quarkusio/quarkus-super-heroes/blob/main/rest-heroes/src/test/java/io/quarkus/sample/superheroes/hero/repository/HeroRepositoryTests.java