我想知道是否有什么方法可以在每次集成测试后重置数据库没有 @DirtiesContext:
@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD)
This works但它是 非常慢,因为每次测试都会重新加载 Spring 上下文。
我的测试正在使用 MockMvc ,对 API 进行休息调用。喜欢:
mockMvc.perform(put("/products/)
.header("Content-Type", "application/json")
.content(jsonPost))
.andExpect(status().isOk())
.andReturn();
因此,无需人工干预(创建和维护脚本以删除和创建表),Spring 框架提供了一些替代方案吗?
最佳答案
在简单的情况下,将每个测试类注释为 @Transactional
并且事务管理器会在每个 @Test
之后做回滚方法。获取更多信息 reading this .
关于database - 在不使用 DirtiesContext 的情况下在 Spring 的每次测试后重置数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48714118/