如何针对与 Tomcat 不同的数据库运行 Spring Roo 生成的测试?

发布于 2024-08-22 02:03:36 字数 711 浏览 5 评论 0原文

我有 Spring Roo 为我的域对象(和 DAO ITD)生成的集成测试集合。

它们似乎被固定为使用“生产”applicationContext.xml,它读取database.properties并连接到我为试验该项目而设置的MySQL数据库模式:

privileged aspect AdvertIntegrationTest_Roo_IntegrationTest {

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

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

这样做的结果是我的演示数据库经常被填充通过这些测试发现垃圾。

我想更改配置,以便集成测试使用 in-mem 数据库,并单独保留 MySQL 数据库。目前,我能看到的唯一选择是删除 Roo 注释并从现在开始自己管理这些测试,但我现在宁愿让 Roo 留在循环中。

是否可以配置我的项目,以便“mvn tomcat”和“mvn test”命令使用单独的数据库,而不破坏 Spring Roo 设置?或者也许有更好的方法来实现我想做的事情?

I have a collection of integration tests that have been generated by Spring Roo for my domain objects (and DAO ITDs).

They appear to be fixed to use the "production" applicationContext.xml, which reads the database.properties and connects to the MySQL database schema I have set up 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");   

The outcome of this is that my demo database is frequently populated with garbage by these tests.

I'd like to change the configuration so that the integration tests use an in-mem database, and leave the MySQL database well alone. At the moment, the only option that I can see is to remove the Roo annotations and manage these tests myself from now on, but I'd rather keep Roo in the loop at the moment.

Is it possible to configure my project, so the "mvn tomcat" and "mvn test" commands use separate databases, without breaking the Spring Roo set-up? Or perhaps there is a better approach for what I want to do?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

少年亿悲伤 2024-08-29 02:03:36

肖恩,

我也曾为同样的事情而挣扎。我最终将 applicationContext.xml 的副本放在 test/resources/META-INF/spring 中,并修改下面的行:

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

在属性占位符指向的“test”目录中,我放置了另一个配置 hsqldb 的database.properties。

最后,我必须有一个不同的 persistence.xml 副本,它配置 SQL 方言(也在 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>

我认为通过使用 pom.xml 魔法可以提供更优雅的解决方案,但目前这似乎是可以接受的给我解决方案。

汉斯

Sean,

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

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

In that 'test' directory the property place holder points to, i have put another database.properties which configures hsqldb.

Finally, I had to have a different copy of persistence.xml which configures the 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 suppose that a more elegant solution through use of pom.xml magic is possible, but for now this seemed like an acceptable solution to me.

Hans

一身仙ぐ女味 2024-08-29 02:03:36

肖恩,

我遇到了同样的问题,并发现了另一件事可能对所有人都有用。
在 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 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 (这个用于测试)中只需要 2 处更改:

  1. 属性文件从 META-INF/spring-test/ 加载*

    ;
    
  2. persistenceUnitName 指向 persistance.xml 中的“testPersistenceUnit”

    
    <属性名称=“persistenceUnitName”值=“testPersistenceUnit”/>
    <属性名称=“数据源”ref=“数据源”/>
    

希望这会对某人有所帮助,因为那里有很多答案,但很难找到
中定义多个 persistenceUnit

您可以在一个persistence.xml

Sean, all

I had the same problem and found one more thing which might be useful to all.
In persistence.xml one can define multiple persistence-unit with different names like:

    <?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 applicationContext.xml (this one for tests )only 2 changes are needed:

  1. properties files are loaded form META-INF/spring-test/*

    <context:property-placeholder location="classpath*:META-INF/spring-test/*.properties"/>
    
  2. 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 will help someone as there is many answers there but it is hard to find out
that you can have multiple persistenceUnits defined in one persistence.xml

Szymon

江挽川 2024-08-29 02:03:36

对我来说,这些步骤工作得很好:

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)复制文件 applicationContext .xmldatabase.properties 从 src/main/resources/META-INF/spring 到 src/test/resources/META-INF/ spring (如果该文件夹不存在,则创建它)。

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) 重命名文件 src/test/resources/META- INF/spring/applicationContext.xml 从 applicationContext.xmltestApplicationContext.xml 并以如下方式更改其内容(只需更改 database.test 中的数据库引用)以及从 persistenceUnitpersistenceUnitTestpersistenceUnitName 属性值)

<?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 */
    }
}

For me these steps worked fine:

1) Add a new persistence unit in your src/main/resources/META-INF/persistence.xml for your test 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 files applicationContext.xml and database.properties 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 content of the src/test/resources/META-INF/spring/database.properties in 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 file src/test/resources/META-INF/spring/applicationContext.xml from applicationContext.xml to testApplicationContext.xml and change its content in something like this (simply change database references in database.test and the persistenceUnitName property value 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 like this:

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 */
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文