Initializing Javers in a Quarkus Test

44 views Asked by At

I'm using Javers in a Quarkus Application. The setup worked fine, and with a bit of copy&paste from the spring javers integration I was able to get it to run.

I tried to use this in a test:

@QuarkusTest
public class JaversTest {
    @Inject
    Javers javers;

    @Test
    void empty() {

    }
}

This throws the following exception:

Caused by: jakarta.enterprise.context.ContextNotActiveException: Cannot use the EntityManager/Session because neither a transaction nor a CDI request context is active. Consider adding @Transactional to your method to automatically activate a transaction, or @ActivateRequestContext if you have valid reasons not to use transactions.

This is the setup for Javers:

@ApplicationScoped
@RequiredArgsConstructor
public class JaversConfig {
    private final EntityManager entityManager;

    @Produces
    @DefaultBean
    public Javers javers() {
        JaversSqlRepository sqlRepository = SqlRepositoryBuilder.sqlRepository()
                .withConnectionProvider(() -> {
                    var session = entityManager.unwrap(Session.class);
                    var connectionWork = new GetConnectionWork();
                    session.doWork(connectionWork);
                    return connectionWork.connection;
                })
                .withDialect(DialectName.POSTGRES).build();

        return JaversBuilder.javers().registerJaversRepository(sqlRepository).build();
    }

    private static class GetConnectionWork implements Work {
        private Connection connection;

        @Override
        public void execute(Connection connection) {
            this.connection = connection;
        }
    }
}

This successfully extracts the connection from the current Transaction when running the application, but not in the test. The exception occurs when calling session.doWork(...);.

I already tried adding a @Transactional or @ActivateRequestContext annotations to both the test class and method. I also injected the EntityManager in the test, and it's working fine (incl. setup & querying in a BeforeEach).

0

There are 0 answers