2010-02-17 29 views
6

我有一个由Spring Roo为我的域对象(和DAO ITD)生成的集成测试集合。如何运行Spring Roo针对Tomcat的不同数据库生成测试?

他们似乎固定用“生产”的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"); 

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

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

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

+0

更新:我从奔亚历一些帮助在弹簧论坛(http://forum.springsource.org/showthread.php?p=284703#post284703),看来Spring Roo的还没有提供任何内置的支持... – seanhodges 2010-02-18 13:45:45

回答

6

肖恩,

我一直在努力与同样的事情。最后我把applicationContext.xml中的副本,测试/资源/ META-INF /弹簧和修改下面一行:

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

在这种“测试”目录属性占位点,我已经把另一配置hsqldb的database.properties。

最后,我不得不persistence.xml中的不同副本配置SQL方言(也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一个更优雅的解决方案是可能的,但现在这似乎是我可以接受的解决方案。

汉斯

+0

我投了jira问题以及:) – 2010-02-19 10:07:06

+0

这是非常有用的,谢谢!你如何指示Roo测试使用“测试”applicationContext.xml?你修改了生成的AJ文件吗? – seanhodges 2010-02-20 09:44:54

+0

不,我没有修改任何aj文件,这不是必需的,也不推荐,因为它们是由roo完全创建/管理的。 Roo将测试执行延期至maven。 maven只会选择位于test/resources中的配置文件,因此位于类路径中。 – 2010-02-20 17:12:42

1

肖恩,所有

我有同样的问题,发现一个可能是大家有用件事。

<?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. 性质 persistence.xml中一个可以与不同的名称,如定义多个持久性单元文件为 “testPersistenceUnit” 在持久性加载形式META-INF /弹簧测试/ *

    <context:property-placeholder location="classpath*:META-INF/spring-test/*.properties"/> 
    
  2. persistenceUnitName来分。XML

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

希望这将帮助别人是有许多答案有,但它是很难找出 您可以在一个persistence.xml中已经定义了多个persistenceUnits

Szymon

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)applicationContext.xml文件重命名为src/test/resources/META-INF/spring/applicationContext.xmltestApplicationContext.xml并改变其内容是这样的(简单地更改数据库的数据库引用。测试和persistenceUnitpersistenceUnitTestpersistenceUnitName属性值)

<?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 */ 
    } 
} 
相关问题