2

I've got standart maven folder structure:

src/main/java  
src/main/resources  
src/test/java  
src/test/resources  

My appicationContext contains following:

<!-- load properties files -->
<context:property-placeholder location="classpath*:*.properties"/>

I have defined 2 hibernate.properties files - one for src/main/resources and one for src/ test/resources. I have expected that when I will run tests my test hibernate.properties will override production hibernate.properties. Instead of that both files are loaded and production version is used:

Loading properties file from file [D:\projects\video_crawler_v3\out\test\core\hibernate.properties]
Loading properties file from file [D:\projects\video_crawler_v3\out\production\core\hibernate.properties]  

How can I correctly setup my properties files? I'm using Intellij IDEA to compile and run tests

Jonathan
  • 20,053
  • 6
  • 63
  • 70
fedor.belov
  • 22,343
  • 26
  • 89
  • 134
  • Can you add an example of your `hibernate.properties` file? I just want to see the kind of differences between your main and test configuration. Thanks. – Jonathan Nov 27 '12 at 09:33
  • I don't think that naming property placeholder configuration file `hibernate.properties` is a great idea, `hibernate.properties' is typically a configuration file of Hibernate itself and has nothing to do with Spring property place holders. – Boris Treukhov Nov 27 '12 at 09:57

2 Answers2

2

One of the options is Spring Profiles http://blog.springsource.com/2011/02/11/spring-framework-3-1-m1-released/

Put two "properties" versions in your context.xml, eg:

<beans>

   ... your beans

    <beans profile="prod">
        <context:property-placeholder location="classpath:/hibernate.properties" />
    </beans>

    <beans profile="test">
        <context:property-placeholder location="classpath:/test-hibernate.properties" />
    </beans>
</beans>

Activate required profile with -Dspring.profiles.active=test.

Note: use www.springframework.org/schema/beans/spring-beans-3.1.xsd

Evgeniy Dorofeev
  • 133,369
  • 30
  • 199
  • 275
  • You should demonstrate how Spring Profiles is a solution here. Also a caveat: the feature is only available from Spring 3.1 and greater. – Jonathan Nov 27 '12 at 09:40
  • you could use `@ActiveProfiles("test")` instead of `-Dspring.profiles.active=test` if you are using TextContext framework http://static.springsource.org/spring/docs/current/spring-framework-reference/html/testing.html – Boris Treukhov Nov 27 '12 at 10:11
1

The files in src/main/resources are always added to the classpath, even when running the unit tests. See this: Common strategies when defining Spring beans for different environments

Community
  • 1
  • 1
mbelow
  • 1,093
  • 6
  • 11