【问题标题】:How can my Spring integration tests create their own schema based on the model in a HSQL database?我的 Spring 集成测试如何根据 HSQL 数据库中的模型创建自己的模式?
【发布时间】:2014-06-25 17:08:07
【问题描述】:

在 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 回答之后,我将架构名称包含在休眠属性中并创建了一个脚本,其中仅包含:

CREATE SCHEMA TESTING  

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

【问题讨论】:

  • 为什么不删除 create_schema.sql 并让 Hibernate 生成表(使用您已经拥有的 create-drop)?
  • 谢谢。您的评论很有帮助

标签: spring hibernate hsqldb integration-testing


【解决方案1】:

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

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

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

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

【讨论】:

  • 恐怕需要同时创建表和数据
  • @borjab 查看编辑,您可能在 hibernate 创建表之前运行导入脚本...
【解决方案2】:

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

首先。似乎告诉您有一个初始化数据库的脚本会使休眠状态避免创建过程。我将代码嵌入 elemento 更改为:

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

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

其次,如果我希望 Spring 创建我的模式,我的上下文配置有一些问题需要解决。最好的方法是将忽略失败属性设置为“ALL”并查看日志。

第三。将 HSQL 用于一些事情 MySQL 用于其他事情有其自身的问题。语法有时不同,我必须修复我的 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 );
            }           
        }
    }

【讨论】:

    猜你喜欢
    • 2014-04-09
    • 1970-01-01
    • 1970-01-01
    • 2011-04-16
    • 2013-04-15
    • 1970-01-01
    • 1970-01-01
    • 2013-08-22
    • 2018-10-04
    相关资源
    最近更新 更多