如何针对不同的数据库运行Spring Roo生成的测试到Tomcat?

时间:2010-02-17 14:25:57

标签: hibernate spring integration-testing spring-roo

我有一组由Spring Roo为我的域对象(和DAO ITD)生成的集成测试。

它们似乎被修复为使用“production”applicationContext.xml,它读取database.properties并连接到我为实验项目设置的MySQL数据库模式:

privileged aspect AdvertIntegrationTest_Roo_IntegrationTest {

    declare @type: AdvertIntegrationTest: @RunWith
        (SpringJUnit4ClassRunner.class);    

    declare @type: AdvertIntegrationTest: @ContextConfiguration
        (locations = "classpath:/META-INF/spring/applicationContext.xml");   

结果是我的演示数据库经常被这些测试填充垃圾。

我想更改配置,以便集成测试使用内存数据库,并使MySQL数据库保持独立。目前,我能看到的唯一选择是从现在开始删除Roo注释并自行管理这些测试,但我现在宁愿让Roo保持在循环中。

是否可以配置我的项目,因此“mvn tomcat”和“mvn test”命令使用单独的数据库,而不会破坏Spring Roo设置?或者对我想做的事情有更好的方法吗?

3 个答案:

答案 0 :(得分:6)

肖恩,

我一直在努力做同样的事情。我最终在test / resources / META-INF / spring中添加了applicationContext.xml的副本,并修改了以下行:

<context:property-placeholder location="classpath*:META-INF/spring/test/*.properties"/>

在属性占位符指向的'test'目录中,我已经放置了另一个配置hsqldb的database.properties。

最后,我必须有一个不同的persistence.xml副本来配置SQL Dialect(也在applicationContext.xml中)

<bean class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" id="entityManagerFactory">
    <property name="persistenceXmlLocation" value="classpath:META-INF/persistence-for-tests.xml"/>
    <property name="dataSource" ref="dataSource"/>
</bean>

我认为通过使用pom.xml魔法可以实现更优雅的解决方案,但是现在这对我来说似乎是一个可以接受的解决方案。

汉斯

答案 1 :(得分:1)

肖恩,全部

我遇到了同样的问题,发现了一件可能对所有人都有用的事情。 在persistence.xml中,可以定义多个具有不同名称的持久性单元,如:

    <?xml version="1.0" encoding="UTF-8" standalone="no"?>
    <persistence xmlns="http://java.sun.com/xml/ns/persistence"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.0"
    xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
    http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">

    <!-- production persistence unit -->
    <persistence-unit name="persistenceUnit" transaction-type="RESOURCE_LOCAL">
    ...
    </persistence-unit>
    <!-- test persistence unit -->
    <persistence-unit name="testPersistenceUnit" transaction-type="RESOURCE_LOCAL">
    ...
    </persistence-unit>
    </persistence>

然后在您的applicationContext.xml(这个用于测试)中,只需要进行2次更改:

  1. 属性文件从META-INF / spring-test / *

    加载
    <context:property-placeholder location="classpath*:META-INF/spring-test/*.properties"/>
    
  2. persistenceUnitName指向persistance.xml中的“testPersistenceUnit”

    <bean class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" id="entityManagerFactory">
    <property name="persistenceUnitName" value="testPersistenceUnit"/>
    <property name="dataSource" ref="dataSource"/>
    

  3. 希望这会对某人有所帮助,因为那里有很多答案,但很难找到答案 您可以在一个persistence.xml中定义多个persistenceUnits

    Szymon

答案 2 :(得分:0)

对我来说,这些步骤运作良好:

1)在src/main/resources/META-INF/persistence.xml中添加一个新的持久性单元用于测试目的:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.0" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
   <!-- Production Database -->
   <persistence-unit name="persistenceUnit" transaction-type="RESOURCE_LOCAL">
      <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
      <properties>
         <property name="hibernate.dialect" value="org.hibernate.dialect.H2Dialect" />
         <!-- value="create" to build a new database on each run; value="update" to modify an existing database; value="create-drop" means the same as "create" but also drops tables when Hibernate closes; value="validate" makes no changes to the database -->
         <property name="hibernate.hbm2ddl.auto" value="update" />
         <property name="hibernate.ejb.naming_strategy" value="org.hibernate.cfg.ImprovedNamingStrategy" />
         <property name="hibernate.connection.charSet" value="UTF-8" />
         <!-- Uncomment the following two properties for JBoss only -->
         <!-- property name="hibernate.validator.apply_to_ddl" value="false" /-->
         <!-- property name="hibernate.validator.autoregister_listeners" value="false" /-->
      </properties>
   </persistence-unit>

   <!-- Test Database -->
   <persistence-unit name="persistenceUnitTest" transaction-type="RESOURCE_LOCAL">
      <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
      <properties>
         <property name="hibernate.dialect" value="org.hibernate.dialect.H2Dialect" />
         <!-- value="create" to build a new database on each run; value="update" to modify an existing database; value="create-drop" means the same as "create" but also drops tables when Hibernate closes; value="validate" makes no changes to the database -->
         <property name="hibernate.hbm2ddl.auto" value="create" />
         <property name="hibernate.ejb.naming_strategy" value="org.hibernate.cfg.ImprovedNamingStrategy" />
         <property name="hibernate.connection.charSet" value="UTF-8" />
         <!-- Uncomment the following two properties for JBoss only -->
         <!-- property name="hibernate.validator.apply_to_ddl" value="false" /-->
         <!-- property name="hibernate.validator.autoregister_listeners" value="false" /-->
      </properties>
   </persistence-unit>
</persistence>

2)将文件applicationContext.xmldatabase.propertiessrc/main/resources/META-INF/spring复制到src/test/resources/META-INF/spring(如果此文件夹不存在则创建它)。

3)用以下内容替换src/test/resources/META-INF/spring/database.properties的内容:

#Updated at Sat Sep 12 22:13:10 CEST 2015
#Sat Sep 12 22:13:10 CEST 2015
database.test.driverClassName=org.h2.Driver
database.test.url=jdbc:h2:./src/test/resources/db/data
database.test.username=sa
database.test.password=

4)将文件src/test/resources/META-INF/spring/applicationContext.xmlapplicationContext.xml重命名为testApplicationContext.xml并更改其内容(只需更改database.test和persistenceUnitName属性中的数据库引用价值从persistenceUnitpersistenceUnitTest

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd         http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd         http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd         http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">
    <context:property-placeholder location="classpath*:META-INF/spring/*.properties"/>
    <context:spring-configured/>
    <context:component-scan base-package="com.jitter.finance.analyzer">
        <context:exclude-filter expression=".*_Roo_.*" type="regex"/>
        <context:exclude-filter expression="org.springframework.stereotype.Controller" type="annotation"/>
    </context:component-scan>
    <bean class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close" id="dataSource">
        <property name="driverClassName" value="${database.test.driverClassName}"/>
        <property name="url" value="${database.test.url}"/>
        <property name="username" value="${database.test.username}"/>
        <property name="password" value="${database.test.password}"/>
        <property name="testOnBorrow" value="true"/>
        <property name="testOnReturn" value="true"/>
        <property name="testWhileIdle" value="true"/>
        <property name="timeBetweenEvictionRunsMillis" value="1800000"/>
        <property name="numTestsPerEvictionRun" value="3"/>
        <property name="minEvictableIdleTimeMillis" value="1800000"/>
    </bean>
    <bean class="org.springframework.orm.jpa.JpaTransactionManager" id="transactionManager">
        <property name="entityManagerFactory" ref="entityManagerFactory"/>
    </bean>
    <tx:annotation-driven mode="aspectj" transaction-manager="transactionManager"/>
    <bean class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" id="entityManagerFactory">
        <property name="persistenceUnitName" value="persistenceUnitTest"/>
        <property name="dataSource" ref="dataSource"/>
    </bean>
</beans>

5)最后你可以像这样测试你的课程:

import org.junit.Test;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests;

@ContextConfiguration(locations = {"classpath*:/META-INF/spring/testApplicationContext*.xml"})
public class QuoteListTest extends AbstractJUnit4SpringContextTests {
    @Test
    public void checkQuote(){
        /* some code to test, this will interact with the defined database.test */
    }
}