英文:
Recommended way to reset in memory H2 database for each QuarkusTest
问题
我正在实施一些使用RestAssured和内存中的H2数据库的BDD @QuarkusTest
,我正在寻找在每个测试之后重置内存数据库(删除重建或删除所有实体)的最简单和推荐的方法。
我想避免任何需要手动运行的方式,比如创建自定义的DELETE
脚本来在每个测试之后运行,或者表截断。是否有一种干净且与Quarkus兼容的方式来做到这一点?
编辑:请不要推荐与Flyway或其他第三方依赖相关的任何内容。
编辑2:我曾尝试通过手动删除所有实体来进行快速的初步尝试,但我正在寻找一种更简单和自动化的方法。
英文:
I'm implementing some BDD @QuarkusTest
s using RestAssured and an in-memory H2 database. I'm looking for the simplest and recommended way to reset the in memory database (either drop-recreate or delete all entities everywhere) after each test.
I'd like to avoid any hands on ways like create custom DELETE
scripts that run manually after each test of table truncates. Is there a clean and quarkus-native way to do this?
EDIT: Please do not recommend anything related to Flyway or other 3rd party dependency.
EDIT 2: I did a quick initial hack by dropping all entities manually but I'm looking for something simpler and automatic
@AfterEach
void tearDown() {
try (final EntityManager em = emf.createEntityManager()) {
em.getTransaction().begin();
em.createNativeQuery("SET REFERENTIAL_INTEGRITY FALSE").executeUpdate();
em.getTransaction().commit();
for (EntityType<?> entity : em.getMetamodel().getEntities()) {
em.getTransaction().begin();
final Class<?> managedType = entity.getBindableJavaType();
em.createQuery("FROM " + managedType.getSimpleName(), managedType).getResultList().forEach(em::remove);
em.getTransaction().commit();
}
em.getTransaction().begin();
em.createNativeQuery("SET REFERENTIAL_INTEGRITY FALSE").executeUpdate();
em.getTransaction().commit();
}
catch (Exception e) {
e.printStackTrace();
}
}
答案1
得分: 0
直到提供一个不同而更好的答案,我们将使用以下方法:
@AfterEach
void tearDown() {
try (final EntityManager em = emf.createEntityManager()) {
em.getTransaction().begin();
em.createNativeQuery("SET REFERENTIAL_INTEGRITY FALSE").executeUpdate();
em.getTransaction().commit();
for (EntityType<?> entity : em.getMetamodel().getEntities()) {
em.getTransaction().begin();
final Class<?> managedType = entity.getBindableJavaType();
em.createQuery("FROM " + managedType.getSimpleName(), managedType).getResultList().forEach(em::remove);
em.getTransaction().commit();
}
em.getTransaction().begin();
em.createNativeQuery("SET REFERENTIAL_INTEGRITY FALSE").executeUpdate();
em.getTransaction().commit();
}
catch (Exception e) {
e.printStackTrace();
}
}
英文:
Until a different and better answer is provided we'll go with the hack:
@AfterEach
void tearDown() {
try (final EntityManager em = emf.createEntityManager()) {
em.getTransaction().begin();
em.createNativeQuery("SET REFERENTIAL_INTEGRITY FALSE").executeUpdate();
em.getTransaction().commit();
for (EntityType<?> entity : em.getMetamodel().getEntities()) {
em.getTransaction().begin();
final Class<?> managedType = entity.getBindableJavaType();
em.createQuery("FROM " + managedType.getSimpleName(), managedType).getResultList().forEach(em::remove);
em.getTransaction().commit();
}
em.getTransaction().begin();
em.createNativeQuery("SET REFERENTIAL_INTEGRITY FALSE").executeUpdate();
em.getTransaction().commit();
}
catch (Exception e) {
e.printStackTrace();
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论