测试 Hibernate DAO,而不围绕它构建宇宙
我们有一个使用 spring/Hibernate/MySQL 构建的应用程序,现在我们想要测试 DAO 层,但这里有一些我们面临的缺点。
考虑多个对象相互连接的用例,例如:书有页面。
- 如果没有 Book,Page 对象就无法存在,因为 book_id 是 Page 中的强制 FK。
- 为了测试页面,我必须创建一本书。
这个简单的用例很容易管理,但是如果您开始构建一个库,除非您不创建围绕书籍和页面的整个宇宙,否则您无法测试它!
所以要测试Page;
- 创建图书馆
- 创建部分
- 创建流派
- 创建作者
- 创建书籍
- 创建页面
- 现在测试页面。
有没有一种简单的方法可以绕过这个“宇宙创建”并单独测试页面对象。我还希望能够测试与Page相关的HQL。例如:
SELECT new com.test.BookPage (book.id, page.name) FROM Book book, Page page.
JUnit 应该独立运行,因此我必须编写代码来构建测试用例中的所有支持对象来创建页面。有关如何加速该过程的任何提示。
编辑:Spring遵循测试运行后事务回滚的理念,从而恢复所有更改。随着我们的进一步开发,预计架构会发生变化,我希望能够定期针对生产数据库(备份!)进行测试。
We have an application built using spring/Hibernate/MySQL, now we want to test the DAO layer, but here are a few shortcomings we face.
Consider the use case of multiple objects connected to one another, eg: Book has Pages.
- The Page object cannot exist without the Book as book_id is mandatory FK in Page.
- For testing a Page I have to create a Book.
This simple usecase is easy to manage, but if you start building a Library, till you don't create the whole universe surrounding the Book and Page, you cannot test it!
So to test Page;
- Create Library
- Create Section
- Create Genre
- Create Author
- Create Book
- Create Page
- Now test Page.
Is there an easy way to by pass this "universe creation" and just test the page object in isolation. I also want to be able to test HQLs related to Page. eg:
SELECT new com.test.BookPage (book.id, page.name) FROM Book book, Page page.
JUnit is supposed to run in isolation, so I have to write the code to build all the supporting objects in the test case to create the Page. Any tips on how to accelerate the process.
Edit: Spring follows the philosophy of transaction roll-back after the tests have been run, thereyby reverting all changes. Schema changes are expected as we develop further, I want to be able to test it against the production db (backup!) on a regular basis.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我刚刚完成了一个具有这种精确配置的项目。我们使用替代 HSQLDB 数据库进行单元测试,然后关闭这些测试的架构上的引用完整性,取得了巨大成功。
由于您使用的是 Spring,因此步骤如下:
create-drop
操作。AbstractTransactionalJUnit4SpringContextTests
(宇宙历史上最伟大的抽象类),并使用新的@ContextConfiguration
对该类进行注释。还可以使用 @TransactionConfiguration 注释在事务中运行每个测试并自动回滚。这里有一些参考资料可以帮助您朝这个方向前进:
像往常一样,正确配置配置是最重要的困难的部分。但一旦一切正常,您将成为一名造型单元测试员!
I just finished a project with this exact configuration. We had great success using a stand-in HSQLDB database for the unit tests and then turning off referential integrity on the schema for those tests.
Since you're using Spring, these are the steps:
create-drop
for the schema in this configuration.AbstractTransactionalJUnit4SpringContextTests
, the greatest abstract class in the history of the universe, and annotate the class with your new@ContextConfiguration
. Also use the@TransactionConfiguration
annotation to run each test in a transaction with an automatic rollback.Here's a few references that will get you moving in this direction:
As usual with this stuff, getting the config just right is the hard part. But once it's all working, you'll be a styling unit tester!
Unitils 对 junit 或 testng 的扩展对此提供了很好的支持。它们允许您定义针对您的测试类进行调整的数据集,因此它只需要您的类所看到的宇宙的一部分,然后在测试开始之前初始化数据库。
checkout : 链接文本
我们正在使用它,它工作得很好。比我们之前使用的“MockRepositories”要好得多,它不测试 HQL,而且也很重要的是 hibernate 事务行为。
The Unitils extensions to junit or testng have very nice support for this. They allow you to define datasets tuned for your class under test so it only needs the part of the universe your class is seeing and then it initializes the database before the tests start.
checkout : link text
We are using it and it just works fine. A lot better that the "MockRepositories" which we used before which do not test the HQL and, also important, the hibernate transaction behaviors.