我正在使用JUnit 4测试一个非常简单的Java应用程序.简单"是指没有弹簧,也没有休眠.我需要测试数据访问层(JDBC,MySQL),我怀疑哪种方法更适合这种测试?在@Before上插入数据并在@After上删除数据,还是在@Before上创建事务并在@After上回滚?
I'm testing a very simple java application using JUnit 4. By "simple" I mean there is no spring and no hibernate. I need to test the data access layer (JDBC, MySQL) and my doubt is which approach is better for this kind of test? Insert data on @Before and delete on @After or create a transaction on @Before and rollback on @After?
谢谢!
推荐答案我不同意使用MySQL以外的其他数据库,因为您可能会遇到测试平台差异,这掩盖了您的MySQL代码所存在的问题.如果不进行大量重构,您的某些代码/SQL甚至可能无法在其他平台上运行.
I would disagree with using a DB other than MySQL, as you might be exposed to platform differences in your tests which mask problems your code has with MySQL. Some of your code/SQL might not even work on another platform without hefty refactoring.
但是,在使用事务而不是删除或更新来恢复状态方面与其他人达成共识.
But, agree with others about using transactions rather than deletes or updates to restore state.
一个警告:如果您使用proc,函数等,它们可以在内部执行COMMIT,这可能会使回滚JUnit更改的任何尝试变得无效.也许这不是您要考虑的问题,但也许是其他人要牢记的问题,尤其是在处理从未考虑过单元测试的遗留数据库代码时.
One caveat: if you're using procs, functions, etc, those can do COMMITs internally which could muck up any attempts to rollback JUnit changes. Maybe not an issue for you, but an issue for maybe others to bear in mind, especially when dealing with legacy DB code for which unit testing was never considered.
更多推荐
JUnit:测试DAO
发布评论