春季交易管理测试 [英] Spring Transaction Management Test

查看:78
本文介绍了春季交易管理测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用SpringContextTests测试我的Dao类.
在我的方法类中,我扩展了AbstractTransactionalJUnit4SpringContextTests以便我的测试类与JUnit4集成.我还设置了配置,并在@Before中进行了初始化和数据库清理,在@After中进行了拆解.我的测试课效果很好.

我的问题是,当我运行测试类并在数据库中填充了数据时,原始数据没有回滚,并且我的数据库被清除了.在@Before方法中,我清除数据库并填充数据,以为我可以回滚数据库,但不能回滚.

任何人都可以找到可以正常工作的示例并回滚数据库中的信息.

ADDONS:
我的测试方法中的每个数据库操作都会回滚.但是,在@Before方法中执行super.deleteFromTables("person")并不会回滚数据库中以前的所有数据.

Spring回滚所有CRUD操作,但是在事务不回滚之前清理数据库.

I want to test my Dao Class using the SpringContextTests.
In my method class I extended the AbstractTransactionalJUnit4SpringContextTests in order for my test class to integrate with JUnit4. I have also set up the configurations and made the initialization and database clean up in the @Before and tearDown in the @After. My test class works perfectly.

My problem was, when I run my test class and the database is filled with data, the original data was not rolled back and my database is cleared. In the @Before method, I clear the database and populate data, thinking that I will be able to rollback it but its not.

Can anyone site an example that works and rollbacks information in the database.

ADDONS:
Every database manipulation in my test methods are rolled back. But the execution of super.deleteFromTables("person") in the @Before method did not rollback all the previous data from the database.

Spring rollbacks all the CRUD operations but the database clean up before the transaction do not rollback.

推荐答案

感谢所有回答我的问题的人.我从这些答案中学到了很多东西,但是并不能解决我的问题.
我知道我的测试数据可以进行事务管理,并且可以正常工作.
这是我的错.

我忘记了有关数据库命令的课程,当您在DML语句之后执行DDL语句时,它将自动提交事务.我在DML之后执行DDL,方法是删除所有记录,然后删除表的ALTER AUTO_INCREMENT,这将导致自动提交并永久删除表的所有记录.

解决该场景解决了我的问题.

Thank you to all those who answered my question. I learned a lot from those answers but it didn't solve my problem.
I knew my test data does a transaction management and it does its job properly.
The mistake is on my part.

I forgot the lesson about database commands that when you execute a DDL statement after a DML statement, it will automatically commit the transaction. I executed a DDL after a DML by deleting all record and then ALTER the AUTO_INCREMENT of the table where in it will cause an auto-commit and delete all records of the table permanently.

FIXING THAT SCENARIO SOLVED MY PROBLEM.

这篇关于春季交易管理测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆