Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Quarkus Reactive testing with database operation leading to java.lang.IllegalStateException: No current Vertx context found

I'm trying to test a part of my code that does a database transaction.

@QuarkusTest
class MyServiceTest {

    @InjectSpy
    MyService myService;

    @Test
    void testDatabaseOperation() {
        myService.doSomeDatabaseOperation()
                .invoke(i -> Assertions.assertEquals(0, i.size()))
                .subscribe().withSubscriber(UniAssertSubscriber.create())
                .assertCompleted();
    }

But I'm getting this exception when trying to call .find() of my repository

java.lang.IllegalStateException: No current Vertx context found

Why is there no session created automatically and how would I tell Quarkus to do so?

like image 582
Marian Klühspies Avatar asked Feb 02 '26 19:02

Marian Klühspies


1 Answers

Found out you need to write your tests slightly different and let Quarkus inject an UniAsserter object, allowing you to do async assertions.

@QuarkusTest
class MyServiceTest {

    @InjectSpy
    MyService myService;


    @Test
    @RunOnVertxContext
    void testDatabaseOperation(UniAsserter a) {
        final var asserter = new TransactionalUniAsserterInterceptor(a);

        asserter.assertNotNull(() -> myService.doSomeDatabaseOperation());
    }

}

Additional wrapper that opens transactions

public class TransactionalUniAsserterInterceptor extends UniAsserterInterceptor {

    public TransactionalUniAsserterInterceptor(UniAsserter asserter) {
        super(asserter);
    }

    /**
     * Assert/execute methods are invoked within a database transaction
     */
    @Override
    protected <T> Supplier<Uni<T>> transformUni(Supplier<Uni<T>> uniSupplier) {
        return () -> Panache.withTransaction(uniSupplier);
    }
}

like image 93
Marian Klühspies Avatar answered Feb 04 '26 09:02

Marian Klühspies



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!