stop the application between tests to make sure that there are no active transactions when we truncate the tables

This commit is contained in:
Athou
2026-03-04 13:49:44 +01:00
parent 42d1db5fc3
commit eaa5bc896e
5 changed files with 40 additions and 24 deletions

View File

@@ -6,6 +6,8 @@ import jakarta.persistence.EntityManager;
import org.hibernate.Session;
import org.kohsuke.MetaInfServices;
import io.quarkus.runtime.ShutdownEvent;
import io.quarkus.runtime.StartupEvent;
import io.quarkus.test.junit.callback.QuarkusTestBeforeEachCallback;
import io.quarkus.test.junit.callback.QuarkusTestMethodContext;
@@ -17,12 +19,17 @@ public class DatabaseReset implements QuarkusTestBeforeEachCallback {
@Override
public void beforeEach(QuarkusTestMethodContext context) {
CDI.current()
.select(EntityManager.class)
.get()
.unwrap(Session.class)
.getSessionFactory()
.getSchemaManager()
.truncateMappedObjects();
// stop the application to make sure that there are no active transactions when we truncate the tables
getBean(CommaFeedApplication.class).stop(new ShutdownEvent());
// truncate all tables so that we have a clean slate for the next test
getBean(EntityManager.class).unwrap(Session.class).getSessionFactory().getSchemaManager().truncateMappedObjects();
// restart the application
getBean(CommaFeedApplication.class).start(new StartupEvent());
}
private static <T> T getBean(Class<T> clazz) {
return CDI.current().select(clazz).get();
}
}