针对 HSQLDB 测试 DAO 的 Spring/Hibernate/Junit 示例
我正在尝试实现 JUnit 测试来检查 DAO 的功能。 (DAO 将创建/读取基本的对象/表关系)。
我遇到的麻烦是 DAO(对于非测试代码)的持久性是通过使用 Spring/Hibernate 的内部解决方案来完成的,这消除了通常的 *。我发现的大多数示例都包含 hbm.xml 模板。
因此,我在理解如何设置 JUnit 测试来实现 DAO 以创建/读取内存中 HSQLDB(只是非常基本的功能)时遇到了一些困难>。我找到了一些示例,但是内部持久性的使用意味着我无法扩展示例中显示的某些类(我似乎无法正确获取 application-context.xml 设置)。
任何人都可以建议我可以查看的任何项目/示例(或任何文档),以进一步了解实现此测试功能的最佳方法吗?我觉得这应该非常简单,但在实现我找到的示例时我一直遇到问题。
编辑:
这是我的解决方案,以提高可读性,适合任何需要帮助的人:
我的
TestClass
:@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(位置=“类路径:applicationContextTest-Example.xml”) @事务性 公共类ExampleDaoTest扩展AbstractTransactionalJUnit4SpringContextTests { @Resource(名称 = "sessionFactory") 私有 SessionFactory 示例SessionFactory; @Resource(名称 = "exampleDao") 私有ExampleDao exampleDao;
我的
applicationContext.xml
文件:> <属性名称=“driverClassName”值=“org.hsqldb.jdbcDriver”/> <属性名称=“url”值=“jdbc:hsqldb:mem:ExampleTest”/> <属性名称=“用户名”值=“sa”/> <属性名称=“密码”值=“”/> <属性名称=“dataSource”ref=“example_dataSource”/> <属性名称=“annotatedClasses”> <列表> <值>org.myExample.ExampleClass <属性名称=“hibernateProperties”> ....留给用户选择属性
I'm working on trying to implement a JUnit test to check the functionality of a DAO. (The DAO will create/read a basic object/table relationship).
The trouble I'm having is the persistence of the DAO (for the non-test code) is being completed through an in-house solution using Spring/Hibernate, which eliminates the usual *.hbm.xml
templates that most examples I have found contain.
Because of this, I'm having some trouble understanding how to setup a JUnit test to implement the DAO to create/read (just very basic functionality) to an in-memory HSQLDB. I have found a few examples, but the usage of the in-house persistence means I can't extend some of the classes the examples show (I can't seem to get the application-context.xml setup properly).
Can anyone suggest any projects/examples I could take a look at (or any documentation) to further my understanding of the best way to implement this test functionality? I feel like this should be really simple, but I keep running into problems implementing the examples I have found.
edit:
Here's my solution for better readability, for anyone who needs a hand getting things going:
My
TestClass
:@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = "classpath:applicationContextTest-Example.xml") @Transactional public class ExampleDaoTest extends AbstractTransactionalJUnit4SpringContextTests { @Resource(name = "sessionFactory") private SessionFactory exampleSessionFactory; @Resource(name = "exampleDao") private ExampleDao exampleDao;
My
applicationContext.xml
file:<!-- List of Daos to be tested --> <bean id="exampleDao" class="org.myExample.ExampleDao"/> <!-- Datasource --> <bean id="example_dataSource" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value="org.hsqldb.jdbcDriver"/> <property name="url" value="jdbc:hsqldb:mem:ExampleTest"/> <property name="username" value="sa"/> <property name="password" value=""/> </bean> <!-- Session Factory --> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"> <property name="dataSource" ref="example_dataSource"/> <property name="annotatedClasses"> <list> <value>org.myExample.ExampleClass</value> </list> </property> <property name="hibernateProperties"> .... left to user to choose properties </property> </bean>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
Spring 3 提供了新的
jdbc
命名空间,其中包括对嵌入式数据库(包括 HSQLDB)的支持。这样就解决了那部分。我想知道“内部解决方案”可能是什么。您可以使用注释(JPA 或 Hibernate 注释)来 ORM 您的域对象,那么为什么需要“内部解决方案”呢?例如:
就实现测试而言,使用 Spring 的 TestContext 框架。测试可以如下所示(我再次假设下面是 Spring 3,尽管只需将 @Inject 更改为 @Autowired 就可以在 Spring 2.5 中工作):
您可以将嵌入式数据库放入 beans-datasource-it 中。例如,xml。 (这里的“it”代表集成测试,文件位于类路径中。)此示例中的控制器位于
beans-web.xml
中,并将自动连接到ContactController< /代码> 字段。
这只是要做的事情的概述,但希望它足以帮助您开始。
Spring 3 offers a new
jdbc
namespace that includes support for embedded databases, including HSQLDB. So that takes care of that part.I'm wondering what the "in-house solution" could be. You can use annotations (either JPA or Hibernate annotations) to ORM your domain objects, so why do you need an "in-house solution"? E.g.:
As far as implementing a test goes, use Spring's TestContext Framework. A test can look like this (again I'm assuming Spring 3 below, though it should work in Spring 2.5 simply by changing @Inject to @Autowired):
You'd put the embedded database inside
beans-datasource-it.xml
, for example. ('it' here stands for integration test, and the files are on the classpath.) The controller in this example lives inbeans-web.xml
, and will be autowired into theContactController
field.That's just an outline of what to do but hopefully it's enough to get you started.
请参见此处。它假定 maven2 作为构建工具,但您可以轻松使用任何东西。
See here. It assumes maven2 as build tool, but you can easily use anything.
我最近使用 Hibernate、Spring 和 HSQLDB 在一些代码中实现了类似的解决方案。
值得注意的是,AbstractTransactionalJUnit4SpringContextTests /code> 现在已被弃用 - 但测试仍然非常简单 - 我在这里介绍了大部分细节: http://automateddeveloper.blogspot.com/2011/05/hibernate-spring-testing-dao-layer-with.html
I have recently implemented a similar solution in some of my code using Hibernate, Spring and HSQLDB.
Its is worth noting that
AbstractTransactionalJUnit4SpringContextTests
has now be deprecated - but it is still pretty straight forward to test - I cover most the details here: http://automateddeveloper.blogspot.com/2011/05/hibernate-spring-testing-dao-layer-with.htmlhibernate 的底线是 SessionFactory - 您的内部解决方案很可能会以某种方式创建其中之一。了解如何操作,然后添加一个 bean,以相同的方式在您的测试应用程序上下文中创建一个 bean(或者如果可能的话,使用运行时使用的内部代码)。您可能需要创建自己的 FactoryBean 来进行实例化。 (使用 AbstractFactoryBean 作为您的基类。)
一旦就位,大多数使用 LocalSessionFactoryBean 的示例都可以迁移到您的情况 - 而不是使用 LocalsessionFactoryBean,而是使用您的自定义工厂 bean。
(如果您还没有这样做,请查看 测试spring 参考中的 部分 - 它使得使用 Spring 进行测试,并使用上下文中的 bean 注入测试变得轻而易举。)
The bottom line with hibernate is the
SessionFactory
- your in-house solution will most likely be creating one of these somehow. Find out how, and then add a bean to create one in your test app context in the same way (or if possible using your in-house code that is used at runtime.). You may need to create your own FactoryBean to do the instantiation. (Use AbstractFactoryBean as your base class.)Once this is in place, most of the examples using LocalSessionFactoryBean can be migrated to your situation - instead of using LocalsessionFactoryBean, use your custom factory bean.
(If you've not done so already, look at the Testing section in the spring reference - it makes testing with Spring, and injecting tests with beans from the context a breeze.)
我的应用程序上下文看起来有点不同
,我的测试类如下所示:
My application context looks a bit different
and my test class looks like this: