简体   繁体   English

测试Hibernate映射的最佳实践

[英]Best practice for testing Hibernate mappings

I am wondering what people have found their best practice to be for testing Hibernate mappings and queries ? 我想知道人们已经找到了最佳实践来测试Hibernate映射和查询?

This cannot be done with Unit Testing, so my experience has been to write Integration Tests that solely tests the DAO layer downwards. 这不能通过单元测试完成,所以我的经验是编写集成测试,仅向下测试DAO层。 This way I can fully test each Insert / Update / Delete / ReadQueries without testing the full end-to-end solution. 这样我就可以完全测试每个Insert / Update / Delete / ReadQueries,而无需测试完整的端到端解决方案。

Whenever the Integration test suite is run it will:- 无论何时运行Integration测试套件,它都将: -

  1. Drop and re-create the database. 删除并重新创建数据库。
  2. Run an import SQL script that contains a subset of data. 运行包含数据子集的导入SQL脚本。
  3. Run each test in a Transactional context that rolls back the transaction. 在事务上下文中运行每个测试以回滚事务。 Therefore it can be run multiple times as an independent test, or and as part of a suite, and the same result is returned as the database is always in a known state. 因此,它可以作为独立测试运行多次,或作为套件的一部分运行,并且由于数据库始终处于已知状态,因此返回相同的结果。

I never test against a different "in memory" database, as there is always an equivalent development database to test against. 我从不测试不同的“内存”数据库,因为总会有一个等价的开发数据库来测试。

I have never had the need to use DBUnit. 我从来没有需要使用DBUnit。

Never use DbUnit for this. 永远不要使用DbUnit。 It's way too much overhead for this level of testing. 对于这种级别的测试来说,这是太多的开销。

Especially if you're using Spring in your app, check out the Spring Test Framework to help manage your data-access tests, particularly the transaction management features . 特别是如果您在应用程序中使用Spring,请查看Spring Test Framework以帮助管理数据访问测试,尤其是事务管理功能

An "equivalent development database" is fine, but an in-memory H2 database will blow away anything else for speed. “等效的开发数据库”很好,但是内存中的H2数据库会为了速度而吹走其他任何东西。 That's important because, while the unit/integration status of these tests may be contested, they're tests you want to run a lot, so they need to be as fast as possible. 这很重要,因为虽然这些测试的单元/集成状态可能存在争议,但它们是您想要运行很多的测试,因此它们需要尽可能快。

So my DAO tests look like this: 所以我的DAO测试看起来像这样:

  1. Spring manages the SessionFactory and TransactionManager . Spring管理SessionFactoryTransactionManager
  2. Spring handles all transactions for test methods. Spring处理测试方法的所有事务。
  3. Hibernate creates the current schema in an in-memory H2 database. Hibernate在内存H2数据库中创建当前模式。
  4. Test all the save, load, delete, and find methods, doing field-for-field comparison on before and after objects. 测试所有保存,加载,删除和查找方法,对前后对象进行逐场比较。 (Eg create object foo1 , save it, load it as foo2 , verify foo1 and foo2 contain identical values.) (例如,创建对象foo1 ,保存它,将其加载为foo2 ,验证foo1foo2包含相同的值。)

Very lightweight and useful for quick feedback. 非常轻巧,有助于快速反馈。

If you don't depend on proprietary rdbms features (triggers, stored procedures etc) then you can easily and fully test your DAOs using JUnit and an in memory database like HSQLDB. 如果您不依赖于专有的rdbms功能(触发器,存储过程等),那么您可以使用JUnit和内存数据库(如HSQLDB)轻松地完全测试您的DAO。 You'll need some rudimentary hibernate.cfg.xml emulation via a class (to initialize hibernate with HSQLDB, load the hbm.xml files you want) and then pass the provided datasource to your daos. 你需要通过一个类进行一些基本的hibernate.cfg.xml仿真(用HSQLDB初始化hibernate,加载你想要的hbm.xml文件),然后将提供的数据源传递给你的daos。

Works well and provides real value to the development lifecycle. 运行良好,为开发生命周期提供真正的价值。

The way I do it is pretty similar with your own, with the exception of actually using in-memory data-bases, like HSQLDB. 除了实际使用内存数据库(如HSQLDB)之外,我的方式与您自己的方式非常相似。 It's faster and more portable than having a real database configured (one that runs in a standalone server). 它比配置真实数据库(在独立服务器中运行的数据库)更快,更便携。 It's true that for certain more advanced features HSQLDB won't work as it simply does not support them, but I've noticed that I hardly run into those when just integration testing my data access layer. 确实,对于某些更高级的功能,HSQLDB不能正常工作,因为它根本不支持它们,但我注意到,当我只是对我的数据访问层进行集成测试时,我很难遇到这些功能。 However if this is the case, I like to use the "jar" version of mysql, which allows me to start a fully functional MYSql server from java, and shut it down when I'm done. 但是,如果是这种情况,我喜欢使用mysql的“jar”版本,它允许我从java启动一个功能齐全的MYSql服务器,并在我完成时将其关闭。 This is not very practical as the jar file is quite big : 这不是很实用,因为jar文件非常大:

http://dev.mysql.com/doc/refman/5.0/en/connector-mxj-configuration-java-object.html http://dev.mysql.com/doc/refman/5.0/en/connector-mxj-configuration-java-object.html

but it's still useful in some instances. 但它在某些情况下仍然有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM