0

I'm using maven build helper to automatically add integration-test/java and integration-test/resources folders to my project classpath. It works great when I run my tests with JUnit in Eclipse, but got a problem when I ran the web project where Tomcat always pick up the test resources instead of the production one.

My project structure is something like this:

DaoProject
    src/main/resources/datasource.properties
    src/main/resources/spring-data.xml
    src/main/java/....
WebProject
    src/main/resources/appContext.xml
    src/integration-test/java/com/mypkg/controller/MyControllerIT.java
    src/integration-test/resources/datasource.properties

The appContext.xml imports spring-data.xml which in turn loads datasource.properties as a propterties source place holder.

appContext.xml contents:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:tx="http://www.springframework.org/schema/tx"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"         xmlns:context="http://www.springframework.org/schema/context"
   xmlns:p="http://www.springframework.org/schema/p" xmlns:mvc="http://www.springframework.org/schema/mvc"
   xsi:schemaLocation="http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx-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/mvc 
        http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">


<context:annotation-config />
<tx:annotation-driven/>
<context:component-scan base-package="com.mypkg" />
<mvc:annotation-driven />
<mvc:resources mapping="/resources/**" location="/resources/" />
<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource"
    p:defaultEncoding="UTF-8">
      <property name="basenames">
       <array>
        <value>messages</value>
       </array>
      </property>
</bean>
<import resource="classpath:datasource.xml" />  

datasource.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation=
"http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd"
    >
<context:property-placeholder location="classpath:datasources.properties"/>

<bean id="dataSource"
    class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="org.postgresql.Driver" />
    <property name="url" value="${config.db.url}" />
    <property name="username" value="${config.db.username}" />
    <property name="password" value="${config.db.password}" />

pom.xml:

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>build-helper-maven-plugin</artifactId>
    <version>1.7</version>
    <executions>
        <execution>
            <id>add-integration-test-sources</id>
            <phase>generate-test-sources</phase>
            <goals>
                <goal>add-test-source</goal>
            </goals>
            <configuration>
                <sources>
                    <source>src/integration-test/java</source>
                </sources>
            </configuration>
        </execution>
        <execution>
            <id>add-integration-test-resources</id>
            <phase>generate-test-resources</phase>
            <goals>
                <goal>add-test-resource</goal>
            </goals>
            <configuration>
                <resources>
                    <resource>
                        <directory>src/integration-test/resources</directory>
                    </resource>
                </resources>
            </configuration>
        </execution>
    </executions>
</plugin>
dnang
  • 939
  • 2
  • 12
  • 17

1 Answers1

0

I think that the problem is with the way you load the datasource.properties file. Try specifying the entire path in the location attribute of the PropertyPlaceholderConfigurer:

<context:property-placeholder location="classpath:src/main/resources/datasources.properties"/>    

Hope this helps.

AxxA Osiris
  • 1,219
  • 17
  • 26
  • Thank you but actually I don't have problem with loading the wrong datasource.xml, as I have only one datasource.xml. It is datasource.properties I have issue with, because I have 2 properties files with the same name. – dnang Oct 17 '13 at 08:13
  • Oh, my mistake. But the same thing applies to that as well. Specify the entire path. I'll edit my answer. – AxxA Osiris Oct 17 '13 at 08:20
  • I know, but the problem is I want to use my test properties file as well. If I use the full path like you suggested, Eclipse will not pick up my test resources when I run my test class (e.g. MyControllerIT.java) – dnang Oct 17 '13 at 09:00