本文介绍了Spring / Hibernate / Junit测试DAO与HSQLDB的例子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试实施 JUnit 测试来检查DAO的功能。 (DAO将创建/读取一个基本的对象/表关系)。

我遇到的麻烦是DAO的持久性(对于非测试代码)通过一个使用 Spring / Hibernate 的内部解决方案完成,它消除了我发现的大多数例子的通常的 *。hbm.xml 模板包含。



因此,我在理解如何设置 JUnit 测试来实现DAO来创建/读取只是非常基本的功能)到内存中的 HSQLDB 。我发现了几个例子,但是使用内部持久性意味着我无法扩展一些示例显示的类(我似乎无法正确获取application-context.xml设置)。



任何人都可以提出任何项目/示例我可以看看(或任何文档)以进一步理解实现此测试功能的最佳方式?我觉得这应该非常简单,但我仍然遇到执行我找到的示例的问题。



这是我的解决方案,为了提高可读性,对于需要手动操作的人来说:


  • 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;


  • 我的 applicationContext.xml 文件:

     <! - 要测试的Daos列表 - > 
    < bean id =exampleDaoclass =org.myExample.ExampleDao/>

    < bean id =example_dataSource
    class =org.apache.commons.dbcp.BasicDataSource>
    < property name =driverClassNamevalue =org.hsqldb.jdbcDriver/>
    < property name =urlvalue =jdbc:hsqldb:mem:ExampleTest/>
    < property name =usernamevalue =sa/>
    < property name =passwordvalue =/>
    < / bean>

    < bean id =sessionFactoryclass =org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean>
    < property name =dataSourceref =example_dataSource/>
    < property name =annotatedClasses>
    < list>
    < value> org.myExample.ExampleClass< / value>
    < / list>
    < / property>
    < property name =hibernateProperties>
    ....留给用户选择属性
    < / property>
    < / bean>



解决方案

Spring 3提供了一个新的 jdbc 命名空间,其中包括对嵌入式数据库(包括HSQLDB)的支持。所以,照顾那部分。



我想知道内部解决方案可能是什么。您可以使用批注(JPA或Hibernate注释)来ORM您的域对象,那么为什么您需要一个内部解决方案?例如:

 < bean id =sessionFactory
class =org.springframework.orm.hibernate3.annotation .AnnotationSessionFactoryBean
p:dataSource-ref =dataSource
p:packagesToScan =myapp.model/>

就实现测试而言,使用Spring的TestContext框架。一个测试可能看起来像这样(我再次假设Spring 3在下面,尽管它应该在Spring 2.5中工作,只需将@Inject更改为@Autowired):

  @RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({
/beans-datasource-it.xml,
/beans-dao.xml,
/beans-service.xml,
/beans-web.xml})
@Transactional
public class ContactControllerIT {
@Inject private ContactController controller ;

... setUp()和tearDown()...

@Test
public void testGetContact(){
String viewName = controller。 getContact(request,1L,model);

...断言...
}
}

例如,您会将嵌入式数据库放在 beans-datasource-it.xml 内。 ('it'代表集成测试,文件位于类路径中。)本例中的控制器位于 beans-web.xml 中,并将自动装入 ContactController 字段。



这只是要做什么的大纲,但希望这足以让您开始。 / p>

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.xmltemplates 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>
    

解决方案

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.:

<bean id="sessionFactory"
    class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"
    p:dataSource-ref="dataSource"
    p:packagesToScan="myapp.model" />

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):

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({
    "/beans-datasource-it.xml",
    "/beans-dao.xml",
    "/beans-service.xml",
    "/beans-web.xml" })
@Transactional
public class ContactControllerIT {
    @Inject private ContactController controller;

    ... setUp() and tearDown() ...

    @Test
    public void testGetContact() {
        String viewName = controller.getContact(request, 1L, model);

        ... assertions ...
    }
}

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 in beans-web.xml, and will be autowired into the ContactController field.

That's just an outline of what to do but hopefully it's enough to get you started.

这篇关于Spring / Hibernate / Junit测试DAO与HSQLDB的例子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-06 04:15