如何有效地使用JUnit和Hibernate?
我想使用JUnit来测试Hibernate代码,例如插入,更新,删除,..方法和事务管理。
但我不知道如何有效地对 Hibernate 应用单元测试以及我应该使用 Hibernate 测试什么。
如何测试 DAO 方法?
希望您能给我一些指导!
I want to use JUnit to test Hibernate code such as insert, update, delete,.. method and transaction management.
But I don't know how to apply unit test for Hibernate usefully and what should I test with Hibernate.
How can I test DAO methods?
Hope that you could give me some guides!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您可以使用DBUnit来测试DAO层。因为你需要数据来测试。
示例: DBUnit xml 将插入虚拟数据到您所描述的数据库,然后您可以调用assertEquals("myname", userDAO.findById(1).getName());测试后,您可以使用DBUnit删除虚拟数据。检查详细信息。
文件
使用 dbunit 进行 Hibernate 测试
DBUnit 和 Hibernate
You can use DBUnit to test DAO Layer. Because you need data to test.
Example : DBUnit xml will insert dummy data to database which is described by you and then you can call assertEquals("myname", userDAO.findById(1).getName()); etc. After test you can delete dummy data with DBUnit. Check detail.
Documents
Hibernate testing with dbunit
DBUnit and Hibernate
您可以使用多种方法,具体取决于您的场景,
@Before
中插入所有需要的数据,在@After
中删除。然而,这并不完全是一个“单元”测试,因为它取决于一些外部先决条件。You can use a number of approaches, depending on your scenario
@Before
, and delete in@After
. This is, however, not exactly a "unit" test, because it depends on some external preconditions.我使用 POJO's in Action 书中描述的 Chris Richardson 的方法
内存 SQL 数据库
优点
缺点
命名查询
优点
缺点
模拟存储库
优点
数量 缺点
DBUnit
优点
缺点
出错,
I use Chris Richardson's approach, described in POJO's in Action book
In-memory SQL database
Pros
Cons
Named queries
Pros
Cons
Mock repositories
Pros
Cons
DBUnit
Pros
Cons
regards,
好的,几点。
首先,如果您必须测试与数据库通信的实际代码,请使用 DBUnit 来让您的生活更轻松,并且建议您使用 HSQLDB,以便您的测试能够设置它们的运行时环境,无需已安装和配置数据库。
其次,如果您不必与数据库对话,我会使用通用模拟库(例如 EasyMock、JMock 或 Mockito),并使测试不真正与数据库对话数据库,这通常会使测试更快更简单。
OK, a few points.
First of all, if you must test actual code that talks to the DB, use DBUnit for making your life easier, and it is recommended that you use HSQLDB, so that your tests will be able to setup their environment on runtime, without requiring a database already being installed and configured.
Second, if you don't have to talk with the DB, I'd use a general mocking library (be it EasyMock, JMock or Mockito, for example), and make the tests not really talk to a DB, which will usually make tests faster and simpler.
就我个人而言,我对将 HSQLDB 等嵌入式数据库与 Hibernate 一起使用非常谨慎,并期望当您将其移动到 Oracle/MySQL/SQL 服务器时一切都会完全相同。 Hibernate 对于这一点来说是一个漏洞太多的抽象。
除了 JUnit 之外,我对其他框架没有任何经验。我发现它做得很好。
以下是我始终牢记的一些事情:
Personally I'm very wary about using embedded databases like HSQLDB with Hibernate and expecting that everything will work exactly the same when you move it to Oracle/MySQL/SQL server. Hibernate is too leaky an abstraction for that.
I have no experience with other frameworks besides JUnit. I find it does the job pretty well.
Here's some things I always bear in mind:
如果您创建了 DAO 对象,则可以向其发送一些对象,调用 save,然后检索这些对象并测试字段值。这是非常基本的 Hibernate 测试。
确保在安装或拆卸方法中清理数据。如果您保存对象,请将其删除。
Rails 的最佳实践是使用单独的数据库进行测试,其中填充了测试数据。我不会针对生产数据库执行此操作;如果您自己有一个可以轻松地重新填充数据的开发数据库,那么就使用它。
If you've created a DAO object, you can then send it some objects, call save, and then retrieve those objects and test the field values. That's very basic Hibernate testing.
Make sure you clean up your data though in your setup or teardown methods. If you save objects, delete them.
Best practices from Rails are to use a separate database for testing, one populated with test data. I wouldn't do this against a production database; if you have a development database for yourself that you can easily repopulate with data, just use that.