使用 hsql hibernate 数据源隔离 Junit 测试

isolate Junit tests using hsql hibernate datasource

我正在为我的 Spring 应用编写 Junit 测试。当我 运行 每个测试单独工作时,但当我 运行 它们作为一个整体时,每个测试的数据都持续存在。测试使用休眠实体生成的 hsql 表。有什么方法可以隔离测试,以便每个测试都处理空表吗?

谢谢!

您可以将您的测试方法或测试 class 标记为 @Transactional:

Annotating a test method with @Transactional causes the test to be run within a transaction that will, by default, be automatically rolled back after completion of the test. If a test class is annotated with @Transactional, each test method within that class hierarchy will be run within a transaction.

更多信息:1, 2