2010-02-17 11 views
6

Ho una raccolta di test di integrazione che sono stati generati da Spring Roo per i miei oggetti dominio (e DAO ITD).Come eseguire i test generati da Spring Roo su un altro database su Tomcat?

sembrano essere fissato per usare la "produzione" applicationContext.xml, che legge i database.properties e si collega allo schema del database MySQL ho predisposto per la sperimentazione con il progetto:

privileged aspect AdvertIntegrationTest_Roo_IntegrationTest { 

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

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

Il Il risultato è che il mio database demo è spesso popolato di spazzatura da questi test.

Mi piacerebbe cambiare la configurazione in modo che i test di integrazione utilizzino un database in-mem e lascino il database MySQL da solo. Al momento, l'unica opzione che posso vedere è di rimuovere le annotazioni Roo e gestire da solo questi test da ora in poi, ma preferirei mantenere Roo nel loop al momento.

È possibile configurare il mio progetto, quindi i comandi "mvn tomcat" e "mvn test" utilizzano database separati, senza interrompere l'installazione di Spring Roo? O forse c'è un approccio migliore per quello che voglio fare?

+0

Update: ho avuto qualche aiuto da Ben Alex sul forum di primavera (http://forum.springsource.org/showthread.php?p=284703#post284703), sembra che la primavera Roo non fornisce ancora qualsiasi supporto integrato per questo ... – seanhodges

risposta

6

Sean,

ho lottato con la stessa cosa. Ho finito per mettere una copia di applicationContext.xml in test/risorse/META-INF/primavera e modificare la linea di seguito:

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

In tale directory 'test' i punti di posto di proprietà di supporto per, ho messo un altro database.properties che configura hsqldb.

Infine, ho dovuto avere una copia diversa di persistence.xml che configura il dialetto SQL (anche 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> 

Suppongo che una soluzione più elegante attraverso l'uso della magia pom.xml è possibile, ma per ora mi è sembrata una soluzione accettabile.

Hans

+0

ho votato anche per il problema di jira :) –

+0

Questo è molto utile, grazie! come hai istruito i test Roo per usare il "test" applicationContext.xml? Hai modificato i file AJ generati? – seanhodges

+0

no non ho modificato alcun file aj, questo non è richiesto e anche non raccomandato poiché sono creati/gestiti esclusivamente da roo. Roo defers esegue l'esecuzione del test come esperto. Maven prenderà semplicemente i file di configurazione che vivono in test/risorse e quindi si trovano sul classpath. –

1

Sean, tutti

ho avuto lo stesso problema e ha trovato una cosa che potrebbe essere utile a tutti. In una persistence.xml possibile definire più persistenza a unità con nomi diversi come:

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

Poi, nel tuo applicationContext.xml (questo uno per le prove) sono necessari solo 2 cambi:

  1. proprietà file vengono caricati i punti modulo META-INF/primavera-test/*

    <context:property-placeholder location="classpath*:META-INF/spring-test/*.properties"/> 
    
  2. persistenceUnitName a "testPersistenceUnit" in persistenza.xml

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

Spero che questo vi aiuterà qualcuno in quanto v'è molte risposte lì, ma è difficile scoprire che è possibile avere più persistenceUnits definita in una persistence.xml

Szymon

0

Per me questi passaggi hanno funzionato correttamente:

1) Aggiungere una nuova unità di persistenza nella vostra src/main/resources/META-INF/persistence.xml per il vostro scopo di test:

<?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) copiare i file e le applicationContext.xmldatabase.propertiessrc/main/resources/META-INF/spring-src/test/resources/META-INF/spring (se questa cartella non esiste crearla).

3) Sostituire il contenuto del src/test/resources/META-INF/spring/database.properties in qualcosa di simile:

#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) Rinominare il file src/test/resources/META-INF/spring/applicationContext.xmlapplicationContext.xml-testApplicationContext.xml e cambiare il suo contenuto in qualcosa di simile (è sufficiente modificare i riferimenti del database nel database. prova e il valore persistenceUnitName immobile dal persistenceUnit al 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) Infine è possibile verificare la classe come questa:

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 */ 
    } 
} 
Problemi correlati