最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

java - Testing Quarkus methods annotated by @WithTransaction - Stack Overflow

programmeradmin2浏览0评论

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 ??

Share Improve this question asked Mar 17 at 13:09 Jan PeremskýJan Peremský 614 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 2

We 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

发布评论

评论列表(0)

  1. 暂无评论