我的Spring集成测试如何根据HSQL数据库中的模型创建自己的模式?

时间:2014-05-08 10:46:56

标签: spring hibernate hsqldb integration-testing

在Spring中编写一些集成测试(就像在this question中一样)我想避免使用大脚本来创建HSQL中的所有表:

<jdbc:script location="classpath:createSchema.sql" />

Spring正在自动地从模型中创建和更新我的开发表。 我怎么能告诉Spring为我的集成测试做同样的事情?

我的数据源文件是

<?xml version="1.0" encoding="UTF-8"?>
<beans 
    xmlns=          "http://www.springframework.org/schema/beans"   
    xmlns:xsi=      "http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context=  "http://www.springframework.org/schema/context" 
    xmlns:jee=      "http://www.springframework.org/schema/jee" 
    xmlns:p=        "http://www.springframework.org/schema/p"
    xmlns:jdbc=     "http://www.springframework.org/schema/jdbc"    
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context               http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/jee                   http://www.springframework.org/schema/jee/spring-jee.xsd
        http://www.springframework.org/schema/jdbc                  http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd">        

    <!-- Usamos HSQLDB para hacer las pruebas con in-memory db -->
    <jdbc:embedded-database id="dataSource" type="HSQL">
        <jdbc:script location="classpath:create_schema.sql"/>
        <jdbc:script location="classpath:import.sql"/>           
    </jdbc:embedded-database> 

    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="packagesToScan" value="com.myProject.model" />
        <property name="hibernateProperties">
        <props>
            <prop key="hibernate.default_schema">TESTING</prop>             
            <prop key="hibernate.dialect">org.hibernate.dialect.HSQLDialect</prop>
            <prop key="hibernate.hbm2ddl.auto">create-drop</prop>               
            <prop key="hibernate.show_sql">true</prop> 
        </props>
        </property>
    </bean>

    <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>
</beans>

更新:在NimChimpsky回答之后,我已将模式名称包含在hibernate属性中并使用以下内容创建了一个脚本:

CREATE SCHEMA TESTING  

在import.sql脚本中尝试第一次插入后,我看到: org.hsqldb.hsqlexception用户缺少未找到的权限或对象[MyTable]

2 个答案:

答案 0 :(得分:2)

您仍然需要手动创建架构 - 但实际上只需要创建架构my-schema-name 语句,让hibernate创建表

<jdbc:embedded-database id="dataSource" type="HSQL">
  <jdbc:script location="classpath:create_schema.sql"/>
</jdbc:embedded-database>

如果使用值填充数据库是一个问题,是的。真的没有捷径。

导入脚本必须在创建表后运行,可能采用postconstruct方法。

答案 1 :(得分:0)

我在 geoand 评论的帮助下解决了这个问题。 (谢谢,你应该把它作为答案发布)。但是,让我分享更多信息,让未来读者的生活更轻松。

首先。似乎告诉你有一个初始化数据库的脚本使得hibernate避免了创建过程。我将嵌入的代码元素更改为:

<jdbc:embedded-database id="dataSource" type="HSQL"/>

Hibernate将自动从类路径执行import.sql(至少在Create或Create-Drop模式下),因此无需指示其他脚本。由于Spring测试框架确实回滚了事务,因此您无需在每次测试后恢复数据库。

其次,如果我想让Spring创建我的架构,我的上下文配置有些问题需要解决。最好的方法是将ignore failures属性设置为&#34; ALL&#34;看看和日志。

第三。使用HSQL做一些事情MySQL for others有其自身的问题。语法有时会有所不同,我必须修复 import.sql 才能与两个数据库兼容。

公寓,它的工作原理:

@ContextConfiguration(locations={"file:src/test/resources/beans-datasource.xml",
                     "file:src/main/webapp/WEB-INF/applicationContext.xml",                                                
                                 "file:src/main/webapp/WEB-INF/my-servlet.xml"})
        @Configuration
        @RunWith(SpringJUnit4ClassRunner.class)
        public static class fooTest{

            @Inject BeanInterface myBean;

            @Test
            @Transactional
            public void fooGetListTest(){

                assertTrue("Expected a non-empty list", myBean.getList().size() > 0 );
            }           
        }
    }