如何有效地使用JUnit和Hibernate?

发布于 2024-08-15 07:08:46 字数 153 浏览 1 评论 0原文

我想使用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

掌心的温暖 2024-08-22 07:08:46

您可以使用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

萌梦深 2024-08-22 07:08:46

您可以使用多种方法,具体取决于您的场景,

  • 使用嵌入式数据库 (HSQLDB) 进行单元测试。在@Before中插入所有需要的数据,在@After中删除。然而,这并不完全是一个“单元”测试,因为它取决于一些外部先决条件。
  • 你可以模拟你的 dao(例如使用 Mokcito),这样它就不会干扰数据库。当测试您的服务层并且您不关心数据库中存储的内容时,这可能很有用。

You can use a number of approaches, depending on your scenario

  • use an embedded database (HSQLDB) for your unit tests. Insert all requried data in @Before, and delete in @After. This is, however, not exactly a "unit" test, because it depends on some external preconditions.
  • you can mock your dao (using Mokcito, for example), so that it does not interfere with the database. This could be useful when testing your service layer and you don't care what is stored in the DB.
迷你仙 2024-08-22 07:08:46

我使用 POJO's in Action 书中描述的 Chris Richardson 的方法

内存 SQL 数据库

优点

  • 没有网络流量
  • 没有磁盘访问
  • 可用于测试查询

缺点

  • 其架构与生产数据库的架构相似吗?

命名查询

优点

  • 可以与存储库分开存储,这样您就可以在没有存储库的情况下对其进行测试

缺点

  • 使用动态查询时效果不佳

模拟存储库

优点

  • 减少数据库访问
  • 减少数量测试用例的

数量 缺点

  • 需要单独测试针对数据库的查询

DBUnit

优点

  • 它是 jUnit 扩展

缺点

  • 您必须建立一个包含预期值的 XML 文件
  • 如果您错过了新的映射属性,则容易

出错,

I use Chris Richardson's approach, described in POJO's in Action book

In-memory SQL database

Pros

  • No network traffic
  • No disc access
  • Useful to test the queries

Cons

  • Is its schema similar to the production database's schema ?

Named queries

Pros

  • Can be stored separately from the repository which allows you to test it without repositories

Cons

  • Does not work fine when using dinamic queries

Mock repositories

Pros

  • Reduces database accesses
  • Reduces the number of test cases

Cons

  • Needs to test queries against the database separately

DBUnit

Pros

  • It is a jUnit extension

Cons

  • You have to seu up an XML file that contains the expected values
  • Error prone if you miss a new mapped property

regards,

老娘不死你永远是小三 2024-08-22 07:08:46

好的,几点。
首先,如果您必须测试与数据库通信的实际代码,请使用 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.

北斗星光 2024-08-22 07:08:46

就我个人而言,我对将 HSQLDB 等嵌入式数据库与 Hibernate 一起使用非常谨慎,并期望当您将其移动到 Oracle/MySQL/SQL 服务器时一切都会完全相同。 Hibernate 对于这一点来说是一个漏洞太多的抽象。

除了 JUnit 之外,我对其他框架没有任何经验。我发现它做得很好。
以下是我始终牢记的一些事情:

  • 数据库 CRUD 操作的单元测试不应该预先假定某些数据的存在或不存在。插入的所有内容也应该删除或回滚。
  • 确保刷新底层连接对象。这将实际执行缓存的语句,并触发数据模型上的任何触发器。

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:

  • Unit tests for database CRUD operations should never presuppose presence or absence of certain data. Everything that is inserted should also be deleted or rolled back.
  • Make sure to flush the underlying connection object. This will actually execute the cached statements, and set off any triggers on the data model.
独闯女儿国 2024-08-22 07:08:46

如果您创建了 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文