如何有用地使用JUnit和Hibernate? [英] How to use JUnit and Hibernate usefully?

查看:128
本文介绍了如何有用地使用JUnit和Hibernate?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用JUnit来测试Hibernate代码,比如插入,更新,删除,..方法和事务管理。

但是我不知道如何如何使用Hibernate的单元测试有用,以及如何使用Hibernate进行测试。



我如何测试DAO方法?



希望你能给我一些指导!

解决方案

您可以使用 DBUnit 来测试DAO层。因为你需要数据来测试。

示例: DBUnit xml会将虚拟数据插入到由您描述的数据库中,然后您可以调用assertEquals(myname,userDAO.findById(1).getName());等等。测试后,您可以使用 DBUnit 删除虚拟数据。检查 详细信息

文档

Hibernate使用dbunit进行测试

DBUnit和Hibernate


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!

解决方案

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

这篇关于如何有用地使用JUnit和Hibernate?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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