Hibernate 单元测试 - 重置架构
我正在 JUnit 测试中测试 DAO 的 CRUD 操作。 当我执行单个测试时,Hibernate 总是重置架构并以已知状态填充数据库。但是,当我在行中执行多个测试时,Hibernate会重置架构一次,然后在测试执行期间累积数据。
这是意外的行为,因此我想在测试的 @Before 方法中添加一个显式重置架构的函数,以避免执行链期间先前测试创建的辅助数据持续存在。
有什么建议吗?
谢谢
I'm testing the CRUD operations of my DAOs in JUnit tests.
When i execute the single test, Hibernate always resets the schema and populates the DB in a known state. But when i execute multiple tests in a row, Hibernate resets the schema once, and then the data is accumulated during the execution of the tests.
This is an unexpected behavior, so I'd like to add in the @Before
method of the tests a function that explicitly resets the schema to avoid the pesistence of side data created by previous tests during the execution chain.
Any tips?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
首先,您所做的不是单元测试,而是集成测试。
Tips:
...
First of all, what you're doing is not unit testing, it's integration testiong.
Tips:
org.springframework.orm.hibernate3.LocalSessionFactoryBean
....
您可以在每个
@Test
方法之前开始一个事务并回滚该事务,如下所示:并在您的测试类中扩展此类。
请注意,这不是最干净的方法,上面的代码更多用于演示目的。
You could begin a transaction before and rollback the transaction each
@Test
method, something like this:And extend this class in your tests classes.
Note that this is not the cleanest way to do this, the code above is more for demonstration purposes.