JUnit:测试DAO

编程入门 行业动态 更新时间:2024-10-25 04:15:36
本文介绍了JUnit:测试DAO-回滚或删除的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在使用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

本文发布于:2023-11-07 00:46:12,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1565099.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:测试   JUnit   DAO

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!