2010-02-17 9 views
6

Mam zestaw testów integracji, które zostały wygenerowane przez Spring Roo dla moich obiektów domeny (i ITD DAO).Jak uruchomić Spring Roo wygenerował testy z inną bazą danych do Tomcat?

Wydają się one być zamocowane do korzystania z „produkcji” applicationContext.xml, który odczytuje database.properties i łączy się z bazą danych MySQL schematu I utworzyły do ​​eksperymentowania z projektu:

privileged aspect AdvertIntegrationTest_Roo_IntegrationTest { 

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

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

The Wynika z tego, że moja baza danych demo jest często zapełniana przez te testy.

Chciałbym zmienić konfigurację, aby testy integracyjne korzystały z bazy danych w bazie i pozostawić bazę danych MySQL w spokoju. W tej chwili jedyną opcją, którą widzę, jest usunięcie adnotacji Roo i zarządzanie tymi testami od teraz, ale wolę teraz trzymać Roo w pętli.

Czy mogę skonfigurować mój projekt, więc komendy "mvn tomcat" i "mvn test" używają oddzielnych baz danych bez przerywania konfiguracji Spring Roo? A może jest lepsze podejście do tego, co chcę zrobić?

+0

Aktualizacja: Mam trochę pomocy od Ben Alex na forach wiosennych (http://forum.springsource.org/showthread.php?p=284703#post284703), wydaje się, że wiosna Roo nie ma jeszcze dostarczyć dowolna wbudowana obsługa tego ... – seanhodges

Odpowiedz

6

Sean,

Walczyłem z tym samym. Skończyło się na umieszczenie kopii applicationContext.xml w test/resources/META-INF/wiosna i modyfikując linię poniżej:

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

W tym katalogu „test” punkty uchwyt miejsce na własność, włożyłem inny database.properties, który konfiguruje hsqldb.

Wreszcie, musiałem mieć inną kopię persistence.xml który konfiguruje dialekt SQL (również w 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> 

Przypuszczam, że bardziej eleganckie rozwiązanie poprzez wykorzystanie magii pom.xml jest możliwe, ale na razie wydawało mi się to możliwym do przyjęcia rozwiązaniem.

Hans

+0

Głosowałem również za sprawą jiry :) –

+0

Jest to bardzo przydatne, dzięki! w jaki sposób poleciłeś testy Roo, aby użyć "testowego" pliku applicationContext.xml? Czy zmodyfikowałeś wygenerowane pliki AJ? – seanhodges

+0

Nie nie modyfikowałem żadnych plików aj, nie jest to wymagane, a także niezalecane, ponieważ są tworzone/zarządzane wyłącznie przez roo. Roo odradza wykonanie testu do maven. użytkownik po prostu pobierze pliki konfiguracyjne, które znajdują się w teście/zasobach, a tym samym znajdują się w ścieżce klas. –

1

Sean, wszystko

miałem ten sam problem i znaleźć jeszcze jedną rzecz, która może być przydatna dla wszystkich. W persistence.xml można zdefiniować wiele trwałości-unit z różnymi nazwami takimi jak:

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

Następnie w applicationContext.xml (w tym jeden dla testów) potrzebne są tylko 2 zmiany:

  1. właściwości pliki są ładowane punktów formą META-INF/wiosna-test/*

    <context:property-placeholder location="classpath*:META-INF/spring-test/*.properties"/> 
    
  2. persistenceUnitName do "testPersistenceUnit" w wytrwałością.xml

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

nadzieję, że pomoże ktoś jak jest tam wiele odpowiedzi, ale trudno jest dowiedzieć się które może mieć wiele persistenceUnits zdefiniowane w jednym persistence.xml

Szymon

0

Dla mnie te kroki działały bez zarzutu:

1) Dodaj nową jednostkę trwałości w src/main/resources/META-INF/persistence.xml dla celów badania:

<?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) Skopiuj pliki applicationContext.xml i database.properties od src/main/resources/META-INF/spring do src/test/resources/META-INF/spring (jeśli ten folder nie istnieje, utwórz go).

3) Wymień treść src/test/resources/META-INF/spring/database.properties w coś takiego:

#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) Zmień nazwę pliku src/test/resources/META-INF/spring/applicationContext.xml z applicationContext.xml do testApplicationContext.xml i zmienić jego treść w czymś takim (wystarczy zmienić referencje bazy danych w bazie danych. Test i wartość persistenceUnitName nieruchomość od persistenceUnit do 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) na koniec można sprawdzić swoją klasę tak:

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 */ 
    } 
} 
Powiązane problemy