2010-02-17 11 views
6

Etki alanı nesneleri (ve DAO ITD'ler) için Spring Roo tarafından oluşturulan bir bütünleştirme testleri koleksiyonum var.Spring Roo nasıl çalıştırılır Tomcat'a farklı bir veritabanına karşı testler oluşturuldu?

privileged aspect AdvertIntegrationTest_Roo_IntegrationTest { 

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

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

The:

Onlar database.properties okur ve ben proje ile deneme için kurdunuz MySQL veritabanı şeması bağlanır "Üretim" applicationContext.xml, kullanımı sabit görünmektedir Bunun sonucu olarak demo veritabanımın bu testlerle sık sık çöplerle doldurulmasıdır.

Yapılandırma işlemini değiştirmek istiyorum, böylece tümleştirme sınamaları yetkili bir veritabanını kullanır ve MySQL veritabanını yalnız bırakır. Şu anda görebildiğim tek seçenek, Roo ek açıklamalarını kaldırmak ve şu andan itibaren bu testleri kendim yönetmek olacak, ama şu anda Roo'yu döngüde tutmayı tercih ediyorum.

Projemi yapılandırmak mümkün mü, "mvn tomcat" ve "mvn sınama" komutları, Spring Roo kurulumunu bozmadan ayrı veritabanları kullanıyorlar mı? Ya da belki de yapmak istediklerim için daha iyi bir yaklaşım var mı?

+0

Güncelleme: Bahar forumlar (http://forum.springsource.org/showthread.php?p=284703#post284703), Ben Alex biraz yardım var, Bahar Roo henüz sağlamaz anlaşılıyor Bunun için herhangi bir yerleşik destek ... – seanhodges

cevap

6

Sean,

Aynı şeyle uğraştım. I koyduk başka özellik yer tutucu noktaları olduğunu 'test' dizinde

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

: I/bahar test/kaynaklar/META-INF applicationContext.xml bir kopyasını koyarak ve aşağıdaki satırı düzenleyerek sona erdi hsqldb'yi yapılandıran database.properties.

Son olarak, herhalde farklı (applicationContext.xml da) SQL Dialect yapılandırır persistence.xml kopyasını

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

olması gerekiyordu pom.xml büyü kullanımı yoluyla daha zarif bir çözüm Mümkün, ama şimdilik bu benim için kabul edilebilir bir çözüm gibi görünüyordu.

Hans

+0

ben jira sorunu için de oy verdi :) –

+0

Bu çok yararlı, teşekkürler! "test" applicationContext.xml dosyasını kullanmak için Roo testlerine nasıl talimat verdiniz? Oluşturulan AJ dosyalarını değiştirdiniz mi? – seanhodges

+0

no herhangi bir aj dosyalarını değiştirmedim, bu zorunlu değildir ve onlar sadece roo tarafından oluşturulduğu/yönetildiği için önerilmez. Roo, yürütme işlemini maven'e erteler. maven sadece test/kaynaklarda bulunan konfigürasyon dosyalarını alır ve böylece sınıf yolundadır. –

1

, Sean

Ben aynı sorunu vardı ve tüm yararlı olabilir bir şey daha buldum. senin applicationContext.xml Sonra

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

sadece 2 değişiklik gerektiğini (testler için bu bir):

  1. özellikleri persistence.xml birinde gibi isimler taşıyan birden kalıcılık-birimini tanımlayabilir dosyaları "testPersistenceUnit" için sebat form META-INF/yay test/*

    <context:property-placeholder location="classpath*:META-INF/spring-test/*.properties"/> 
    
  2. persistenceUnitName noktalarını yüklenir.xml

    <bean class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" id="entityManagerFactory"> 
    <property name="persistenceUnitName" value="testPersistenceUnit"/> 
    <property name="dataSource" ref="dataSource"/> 
    
    orada birçok cevapları ama birden persistenceUnits bir persistence.xml tanımladığınız anlamına dışarı bulmak zor olduğu gibi bu kişiye yardım edecek

Umut

Szymon

0

Benim için bu adımlar iyi çalıştı:

1) Test amaç için src/main/resources/META-INF/persistence.xml yeni kalıcılık birimi ekleyin: bu klasör oluşturun yoksa

<?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) (src/test/resources/META-INF/spring için src/main/resources/META-INF/spring dosyaları applicationContext.xml ve database.properties kopyala).

3) böyle bir şey src/test/resources/META-INF/spring/database.properties içeriğini değiştirin:

#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) (sadece veritabanında veritabanı başvuruları değiştirmek testApplicationContext.xml için applicationContext.xml dosyayı src/test/resources/META-INF/spring/applicationContext.xml yeniden adlandırma ve böyle bir şey, içeriğini değiştirmek. Test ve persistenceUnitTest için persistenceUnit den persistenceUnitName özellik değeri)

<?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) Son olarak böyle sınıfınızı test edebilirsiniz:

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 */ 
    } 
} 
İlgili konular