Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run Spring Roo generated tests against a different database to Tomcat?

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?

like image 287
seanhodges Avatar asked Feb 17 '10 14:02

seanhodges


1 Answers

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

like image 189
Hans Westerbeek Avatar answered Nov 15 '22 11:11

Hans Westerbeek