2010-02-17 13 views
6

मेरे पास एकीकरण परीक्षण का संग्रह है जो मेरे डोमेन ऑब्जेक्ट्स (और डीएओ आईटीडी) के लिए स्प्रिंग रू द्वारा उत्पन्न किया गया है।टॉमकैट के लिए एक अलग डेटाबेस के खिलाफ स्प्रिंग रू उत्पन्न परीक्षण कैसे चलाएं?

privileged aspect AdvertIntegrationTest_Roo_IntegrationTest { 

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

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

:

वे "उत्पादन" applicationContext.xml, जो database.properties पढ़ता है और MySQL डेटाबेस स्कीमा मैं परियोजना के साथ प्रयोग करने के लिए निर्धारित किया है को जोड़ता उपयोग करने के लिए तय होना दिखाई देते हैं इसका नतीजा यह है कि मेरे डेमो डेटाबेस अक्सर इन परीक्षणों से कचरे के साथ आबादी में आते हैं।

मैं कॉन्फ़िगरेशन को बदलना चाहता हूं ताकि एकीकरण परीक्षण इन-मेम डेटाबेस का उपयोग कर सके और MySQL डेटाबेस को अकेले छोड़ दें। फिलहाल, एकमात्र विकल्प जिसे मैं देख सकता हूं, रू एनोटेशन को हटाना और इन परीक्षणों को अब से प्रबंधित करना है, लेकिन मैं इस समय लूप में रू को रखना चाहूंगा।

क्या मेरे प्रोजेक्ट को कॉन्फ़िगर करना संभव है, इसलिए "एमवीएन टॉमकैट" और "एमवीएन टेस्ट" कमांड स्प्रिंग रू सेट अप को तोड़ने के बिना अलग डेटाबेस का उपयोग करते हैं? या शायद मैं क्या करना चाहता हूं इसके लिए एक बेहतर दृष्टिकोण है?

+0

अद्यतन: मैं वसंत मंचों (http://forum.springsource.org/showthread.php?p=284703#post284703) पर बेन एलेक्स से कुछ मदद की है, ऐसा लगता है कि वसंत रू अभी तक प्रदान नहीं करता है इसके लिए कोई अंतर्निहित समर्थन ... – seanhodges

उत्तर

6

शॉन,

मैंने एक ही चीज़ के साथ संघर्ष किया है। करने के लिए, मैं डाल दिया है एक और संपत्ति स्थान धारक अंक

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

कि 'परीक्षण' निर्देशिका में: मैं/वसंत परीक्षा/संसाधन/META-INF में applicationContext.xml की एक प्रति डालने और नीचे लाइन को संशोधित करने के समाप्त हो गया डेटाबेस.प्रोपर्टीज जो hsqldb को कॉन्फ़िगर करता है।

अंत में, मैं जो एसक्यूएल बोली (भी applicationContext.xml में) कॉन्फ़िगर करता है एक अलग persistence.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

मैंने जिरा मुद्दे के लिए भी मतदान किया :) –

+0

यह बहुत उपयोगी है, धन्यवाद! आपने "test" applicationContext.xml का उपयोग करने के लिए रू परीक्षणों को कैसे निर्देश दिया? क्या आपने जेनरेट की गई AJ फ़ाइलों को संशोधित किया था? – seanhodges

+0

नहीं, मैंने किसी भी एजे फाइल को संशोधित नहीं किया है, यह आवश्यक नहीं है और यह भी अनुशंसित नहीं है क्योंकि वे विशेष रूप से roo द्वारा बनाए/प्रबंधित किए जाते हैं। रूयू मैवेन में परीक्षण निष्पादन को रोकता है। मेवेन बस टेस्ट/संसाधनों में रहने वाली कॉन्फ़िगरेशन फ़ाइलों को उठाएगा और इस प्रकार कक्षापथ पर होंगे। –

1

शॉन, सभी

मैं एक ही समस्या थी और एक और बात है जो सभी के लिए उपयोगी हो सकता है पाया। (परीक्षण के लिए यह एक) केवल 2 परिवर्तनों की आवश्यकता है

<?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 में

:

  1. गुण persistence.xml एक में की तरह अलग अलग नामों के साथ एकाधिक हठ इकाई परिभाषित कर सकते हैं फ़ाइलें "testPersistenceUnit" करने के लिए दृढ़ता में प्रपत्र META-INF/वसंत परीक्षण/*

    <context:property-placeholder location="classpath*:META-INF/spring-test/*.properties"/> 
    
  2. persistenceUnitName अंक लोड किए गए हैं।एक्सएमएल

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

आशा इस किसी की मदद के रूप में वहाँ कई जवाब है वहाँ, लेकिन यह है कि आप है कई persistenceUnits एक persistence.xml में परिभाषित कर सकते हैं पता लगाने के लिए मुश्किल है

ज़ीमॉन

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) (src/test/resources/META-INF/spring करने के लिए src/main/resources/META-INF/spring से फाइल applicationContext.xml और database.properties कॉपी करता है, तो इस फ़ोल्डर इसे बनाने के मौजूद नहीं है)।

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) testApplicationContext.xml करने के लिए applicationContext.xml से फ़ाइल src/test/resources/META-INF/spring/applicationContext.xml नाम बदलें और (बस डेटाबेस में डेटाबेस संदर्भ बदलना कुछ इस तरह में अपनी सामग्री बदल जाते हैं। परीक्षण और persistenceUnitTest करने के लिए persistenceUnit से persistenceUnitName संपत्ति के मूल्य)

<?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 */ 
    } 
} 
संबंधित मुद्दे