2

I have this in my pom.xml:

<build>
    <finalName>${project.artifactId}</finalName>
        <filters>
          <filter>${project.basedir}/src/main/resources/filters/${env}.properties</filter>
        </filters>
        <resources>
          <resource>
            <directory>${project.basedir}/src/main/resources</directory>
            <filtering>true</filtering>
          </resource>
        </resources>
    <testResources> 
       <testResource> 
         <directory>${project.basedir}/src/test/resources</directory> 
         <filtering>true</filtering> 
       </testResource> 
    </testResources> 
</build>
    ...
<profiles>
  <profile>
    <id>dev</id>
    <properties>
      <env>dev</env>
    </properties>
    <activation>
      <activeByDefault>true</activeByDefault>
    </activation>
  </profile>
  <profile>
    <id>qa</id>
    <properties>
      <env>qa</env>
    </properties>
  </profile>
  <profile>
    <id>prod</id>
    <properties>
       <env>prod</env>
    </properties>
  </profile>
</profiles>

and I have these files in src/main/resources/filters: dev.properties, qa.properties and prod.properties.

But my spring config test-db-config.xml in src/test/resource is not picking up the values I have in the properties file:

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
    <property name="driverClassName">
        <value>${db.driver}</value>
    </property>
    <property name="url">
        <value>${db.url}</value>
    </property>
    <property name="username">
        <value>${db.usename}</value>
    </property>
    <property name="password">
        <value>${db.password}</value>
    </property>
</bean>

Could someone point me in the right direction? Thanks

This is the error I am getting:

Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 2.185 sec <<< FAILURE!
testCustomerExists(com.xxxx.hcs.persistence.repository.CustomerRepositoryTest) Time elapsed: 1.973 sec  <<< ERROR!
java.lang.IllegalStateException: Failed to load ApplicationContext
    at org.springframework.test.context.CacheAwareContextLoaderDelegate.loadContext(CacheAwareContextLoaderDelegate.java:99)
    at org.springframework.test.context.DefaultTestContext.getApplicationContext(DefaultTestContext.java:101)
    at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:109)
    at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:75)
 ...

In dev.properties I have:

db.driver=oracle.jdbc.OracleDriver
db.url=jdbc:oracle:thin:@hrsdev.xxxx.net:1548:yyyy
db.usedb.passwordrname=xxxx
db.password=yyyy

When I put these values in test-db-config.xml, the test cases run fine. So I know it is caused by the variable substitution.

user3260768
  • 35
  • 1
  • 3
  • 7
  • what is the error message? Also is spelling correct? "db.usenamer" – bgth Apr 10 '14 at 19:29
  • What Spring configuration are you using to include the properties file? – geoand Apr 10 '14 at 19:41
  • I made a typo. corrected db.usename unfortunately it is not what's causing the error. – user3260768 Apr 11 '14 at 12:10
  • pls check this http://stackoverflow.com/questions/17628528/spring-mvc-and-junit-failed-to-load-applicationcontext. Are you using the same appContext for both app and tests? – bgth Apr 11 '14 at 12:48
  • I would suggest that you use Spring Profiles instead of Maven profiles for operations like that. They make working in different environments a breeze and are not dependent on the build tool – geoand May 02 '14 at 13:33

1 Answers1

0

My intuition is that the filter is resolved before the filtering operation, so ${env} variable is not yet valuated and filter is never resolved.

You can test this by directly setting the full name "dev.properties" in the pom which should lead to an effective filtering.

I would advise to use the classical way and directly put your properties values in the profile definition (see http://www.manydesigns.com/en/portofino/portofino3/tutorials/using-maven-profiles-and-resource-filtering).

This does not prevent to use a properties file for the configuration, simply have one single property file (dbConfig.properties) with variables inside and let it be filtered by maven.

[edit]

It seems that the state-of-art approach is using a propertyConfigurer bean, see https://stackoverflow.com/a/11874827/2087640

Community
  • 1
  • 1
Gab
  • 7,869
  • 4
  • 37
  • 68