【发布时间】:2011-03-24 15:43:56
【问题描述】:
我有一个 Spring/JPA Web 应用程序,我想为其编写一些测试。理想情况下,我希望能够:
- 在运行测试之前创建一次测试数据库架构(来自 JPA 注释类)
- 在它自己的事务中运行每个测试方法,当测试完成时回滚
- 在每个类或每个方法级别指定要为每个测试加载的 (DbUnit) 数据集。测试数据应该在事务启动后加载,这样测试完成后测试数据也会回滚
- 将 Spring bean 注入测试类
我知道 Spring 提供的类可以提供我正在寻找的事务行为。理想情况下,最终的解决方案应该是这样的
// This dataset will be used for all tests that don't override it with their own annotation
@TestData('/dbunit/dataSetDefault.xml')
public class MyTests extends ProbablySomethingFromTheSpringFramework {
@Test
void testWithDefaultDataSet() {
// Transaction is implicitly started here
// My test code goes here
// A transaction is implicitly rolled-back here
}
@TestData('/dbunit/dataSetCustom.xml')
@Test
void testWithCustomDataSet() {
// Same as the other test
}
}
显然父类和@TestData 是虚构的,是否有可用的东西提供我正在寻找的功能?
这就留下了如何创建测试数据库架构的问题。理想情况下,这将在所有测试运行之前发生一次(由 Maven)。有人可以建议我如何实现这一目标吗?我想它涉及使用一些东西将 JPA 注释转换为 DDL,然后使用其他东西将其加载到测试数据库模式中。
谢谢!
【问题讨论】:
标签: java maven-2 jpa integration-testing dbunit