title: Spring profiles and testing
body: <p>I've got a web application where I have the typical problem that it requires different configuration files for different environments. Some configuration is placed in the application server as JNDI datasources, however some configuration stays in property files.</p>
  
  <p>Therefore I want to use the Spring profiles feature.</p>
  
  <p>My problem is that I don't get the test case running.</p>
  
  <p>context.xml:</p>
  
  <pre><code>&lt;context:property-placeholder 
    location="classpath:META-INF/spring/config_${spring.profiles.active}.properties"/&gt;
  </code></pre>
  
  <p>Test:</p>
  
  <pre><code>@RunWith(SpringJUnit4ClassRunner.class)
  @TestExecutionListeners({
      TestPreperationExecutionListener.class
      })
  @Transactional
  @ActiveProfiles(profiles = "localtest")
  @ContextConfiguration(locations = {
      "classpath:context.xml" })
  public class TestContext {
  
    @Test
    public void testContext(){
  
    }
  }
  </code></pre>
  
  <p>The problem seems to be that the variable for loading the profile isn't resolved:</p>
  
  <pre><code>Caused by: java.io.FileNotFoundException: class path resource [META-INF/spring/config_${spring.profiles.active}.properties] cannot be opened because it does not exist
  at org.springframework.core.io.ClassPathResource.getInputStream(ClassPathResource.java:157)
  at org.springframework.core.io.support.PropertiesLoaderSupport.loadProperties(PropertiesLoaderSupport.java:181)
  at org.springframework.core.io.support.PropertiesLoaderSupport.mergeProperties(PropertiesLoaderSupport.java:161)
  at org.springframework.context.support.PropertySourcesPlaceholderConfigurer.postProcessBeanFactory(PropertySourcesPlaceholderConfigurer.java:138)
  ... 31 more
  </code></pre>
  
  <p>The current profile should be set with the <code>@ActiveProfile</code> annotation. As it's a test case I won't be able to use the <code>web.xml</code>. If possible I'd like to avoid runtime options as well. The test should run as is (if possible).</p>
  
  <p>How can I properly activate the profile? Is it possible to set the profile with a context.xml? Could I declare the variable in a test-context.xml that is actually calling the normal context?</p>