这可能是一个幼稚的问题,但我对 junit 和 hibernate 框架都是新手,我想知道对主要调用 hibernate 的应用程序进行单元测试的最佳方法是什么,或者是否有必要这样做?
这里的最佳做法是什么?
编辑:
春天似乎是这里的大建议。不幸的是,这对于一个项目来说可能有点太多了。Junit、Hibernate 和 Spring 对我来说都是新的,虽然它们都是我想掌握的技术,但我认为尝试将它们全部整合到一个项目中对我来说可能过于庞大。
欢迎链接到教程和/或书籍建议。
这可能是一个幼稚的问题,但我对 junit 和 hibernate 框架都是新手,我想知道对主要调用 hibernate 的应用程序进行单元测试的最佳方法是什么,或者是否有必要这样做?
这里的最佳做法是什么?
编辑:
春天似乎是这里的大建议。不幸的是,这对于一个项目来说可能有点太多了。Junit、Hibernate 和 Spring 对我来说都是新的,虽然它们都是我想掌握的技术,但我认为尝试将它们全部整合到一个项目中对我来说可能过于庞大。
欢迎链接到教程和/或书籍建议。
Keep in mind the difference between unit testing and integration testing.
Unit tests should be testing code without any outside dependencies. These dependencies are mocked using a framework like, for example, JMock.
Integration tests are important too but the major drawback of them is that they take a long time to run. You can run thousands of true unit tests in a couple of seconds, but it's not the same with integration tests.
Depending on the size of your project/development team you might want to prioritize true unit tests over integration tests. Both style of tests are important but if you are pressed for resources, just going with Unit testing may be a better idea.
I wrote an application by myself that unit tested the Web (with Spring MVC this is easy) and Service layers, as well as domain objects. But I left the DAO alone because I didn't want to write a bunch of slow integration tests. If I had more people on staff I would have gone with integration tests as well, but in this case I didn't feel the time spent would be worth it.
至于最佳实践:
如果可能,请使用嵌入式数据库来运行您的测试,这样您就不需要一个完整部署的关系数据库来运行您的测试(在本地,或者在您的连续构建服务器上,如果有的话)。这样您也不需要(必然)担心回滚等,您可以在需要时重新创建数据库。使用嵌入式数据库进行测试不会测试您使用特定生产数据库时可能出现的特性,但它会测试您的代码,这应该足够了。
在运行 Hibernate 测试之前,您还可以使用JUnit 的扩展DbUnit轻松地用预期的行填充数据库并将其置于已知状态。
最佳实践?我使用 Spring 并使我的所有测试都具有事务性。我执行测试并回滚所有更改,因此我不会更改数据库的状态。
Hibernate 源代码包含大量单元测试,我建议您通过这些测试并采用类似的方法。
您还可以查看示例应用程序为“Java Persistence with Hibernate”一书开发的CaveatEmptor
I like to use a in memory hsqldb for testing. The process for each hibernate POJO is:
For DAOs, I create and persist enough objects to accurately test the methods, then run the tests and delete the objects as necessary to not intefere with other tests.
如果您将 Hibernate 用于域丰富的模型,单元测试域逻辑就像测试 POJO 一样简单,而 Hibernate 不会妨碍您。这里唯一需要注意的是,对于双向映射,您可能必须在两侧设置对象以进行单元测试。
对于简单的映射,通常不进行与数据库的集成测试。但是建议在单表继承等精致映射的情况下使用。这里唯一要记住的是,有时您可能必须显式刷新到数据库。
当然,如果你的持久层不是用 Hibernate 编写的,你会对其进行单元测试,不是吗?
创建使用 Hibernate 实现的给定持久性接口,实例化一些示例对象,执行 CRUD 操作,并要求 JUnit 断言操作成功。与任何其他类相同。
您可以在这里使用 Spring 来提供帮助。
它有一个很棒的单元测试框架,您可以使用它来测试 CRUD 操作,然后回滚更改 - 如果您没有每次都重新加载数据库的能力,那就太好了。
两种情况很容易测试:
如果可行,请在不知道保存或加载实体的函数中执行各种计算和转换。如果你能做出这些纯函数,那就更好了。
对于只保存到数据库而不从数据库中读取的函数,您可以在测试时选择不保存。
执行 #2 的最简单(最粗略)的方法是向reallyUpdate
函数添加一个参数,然后在每个“保存”调用周围加上:
if (reallyUpdate) {
HibernateUtil.saveOrUpdate(theThing);
}
对我来说,这些是最低的挂果。