【发布时间】:2018-12-04 18:45:04
【问题描述】:
编辑:正如 C. Weber 在 cmets 中建议的那样,解决方案是将 @Transactional 添加到测试类中。
我有一些使用 H2 内存数据库的测试。我需要在每次测试之前重置数据库。虽然每次执行测试都会运行我的 SQL 脚本,但数据库没有正确重置,导致删除测试后缺少所需的条目。
测试类:
@RunWith(SpringRunner.class)
@SpringBootTest
@AutoConfigureTestDatabase(replace=Replace.ANY, connection=EmbeddedDatabaseConnection.H2)
public class RepositoryTests {
@Autowired
private Repository repository;
@Autowired
private DataSource dataSource;
@Before
public void populateDb() {
Resource initSchema = new ClassPathResource("database/schema.sql");
Resource initData = new ClassPathResource("database/data.sql");
DatabasePopulator dbPopulator = new ResourceDatabasePopulator(initSchema, initData);
DatabasePopulatorUtils.execute(dbPopulator, dataSource);
}
@Test
public void testMethod1() {
// ...
repository.delete("testdata");
}
@Test
public void testMethod2() {
// ...
Object test = repository.get("testdata");
// is null but should be an instance
}
}
schema.sql 在重新创建它们之前删除所有表。 data.sql 将所有需要的测试数据插入到数据库中。
单独运行testMethod2 成功。但是,运行所有测试会使测试失败并返回 NullPointerException。
我已成功尝试使用@DirtiesContext,但这不是一个选项,因为我无法承受每 0.1 秒的测试需要 20 秒的启动时间。
还有其他解决方案吗?
【问题讨论】:
-
你试过在你的测试类中使用@Transactional吗?
-
哇,成功了!太感谢了。您应该将此作为解决方案发布:)
-
完成。我很高兴能为您提供帮助。
标签: spring-boot junit h2