How to run Spring Roo generated tests against another database for Tomcat?

I have a set of integration tests that Spring Roo created for my domain objects (and DAO ITD).

They seem to be fixed to use the "production" application Context.xml, which reads database.properties and connects to the MySQL database schema that I configured for experimenting with the project:

privileged aspect AdvertIntegrationTest_Roo_IntegrationTest { declare @type: AdvertIntegrationTest: @RunWith (SpringJUnit4ClassRunner.class); declare @type: AdvertIntegrationTest: @ContextConfiguration (locations = "classpath:/META-INF/spring/applicationContext.xml"); 

As a result of this, my demo database is often filled with garbage by these tests.

I would like to reconfigure the integration tests to use the in-mem database and leave the MySQL database alone. For now, the only option I see is to remove Roo annotations and manage these tests from the start, but I would rather keep Roo in a loop for now.

Is it possible to customize my project, so the "mvn tomcat" and "mvn test" commands use separate databases without disturbing the configuration of Roo Spring? Or maybe there is a better approach to what I want to do?

+6
spring spring-roo hibernate integration-testing
source share
3 answers

Sean

I struggled with the same. I ended up putting a copy of applicationContext.xml in test / resources / META-INF / spring and changing the following line:

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

In this directory, "test" indicates the owner of the placement; I added another database that configures hsqldb.

Finally, I had another copy of persistence.xml that sets up SQL Dialect (also in 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> 

I believe that a more elegant solution using pom.xml magic is possible, but so far it seemed to me an acceptable solution.

Hans

+6
source share

Sean, everyone

I had the same problem and I found another thing that could be useful to everyone. In persistence.xml, you can define several persistence units with different names, for example:

  <?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> 

Then in your application Context.xml (this one for tests) only 2 changes are needed:

  • properties files of the form META-INF / spring -test / * are loaded

     <context:property-placeholder location="classpath*:META-INF/spring-test/*.properties"/> 
  • persistenceUnitName points to "testPersistenceUnit" in persistance.xml

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

Hope this helps someone, as there are many answers, but it's hard to know that you can have multiple persistenceUnits defined in one persistence.xml file

Szymon

+1
source share

For me, these steps worked fine:

1) Add a new constant in src/main/resources/META-INF/persistence.xml for your testing purpose:

 <?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) Copy the applicationContext.xml and database.properties files from src/main/resources/META-INF/spring to src/test/resources/META-INF/spring (if this folder does not exist, create it).

3) Replace the contents of src/test/resources/META-INF/spring/database.properties something like this:

 #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) Rename the src/test/resources/META-INF/spring/applicationContext.xml testApplicationContext.xml from applicationContext.xml to testApplicationContext.xml and change its contents something like this (just change the database links in the database.test file and the value of the persistenceUnitName property from persistenceUnit to persistenceUnitTest >)

 <?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) Finally, you can test your class as follows:

 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 */ } } 
0
source share

All Articles