title: Spring's Property Placeholder does not work with jUnit Tests
body: <p>I just configured a property placeholder in my Spring configuration</p>
  
  <pre><code>&lt;context:property-placeholder location="classpath:/config/config.properties" /&gt;
  </code></pre>
  
  <p>If I run the application with this config everything works fine. However if I try to run unit tests, the test fails to load the <code>ApplicationContext</code> because of a <code>FileNotFoundException</code>. This happens if I try to run the tests from Eclipse as well as when running the test via maven.</p>
  
  <p>I also tried to configure the <code>PropertyPlaceholderConfigurer</code> directly with the same result.</p>
  
  <p>It seems as the file is not in the classpath location, even though the test classes are annotated with </p>
  
  <pre><code> @ContextConfiguration("classpath:/config/spring-config.xml")
  </code></pre>
  
  <p>the files are in the same folder and it finds the xml configuration.</p>
  
  <p>I already tried to use different paths: <code>classpath:config/config.properties</code> and without the classpath prefix, all not working. An absolute path with the file prefix works, but thats not a good solution.</p>
  
  <p>Is there a way to make the property-placeholder work with tests? One solution I already found is to override the location by providing default properties in the xml. Is there any other solution? Or am I the only one with this problem?</p>
  
  <p>My test classes look kind of like this:</p>
  
  <pre><code>@RunWith(SpringJUnit4ClassRunner.class)
  @ContextConfiguration("classpath:/config/spring-config.xml")
  @Transactional
  public class JpaImageDaoTest {
  @Autowired
  private ImageDataDao imageDataDao;
  
  @Test
  public void testFindById() {
  
      Image anImage = new Image();
      anImage.setData(new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 });
  
      imageDao.save(anImage);
      Image image = imageDao.findById(imageData.getId());
  
      assertNotNull(image);
      assertEquals(anImage, image);
  }
  </code></pre>
  
  <p>and the context xml looks like this:</p>
  
  <pre><code> &lt;context:property-placeholder location="classpath:/config/config.properties" /&gt;
  
   &lt;bean id="imageScalingService" class="service.image.ImageScalingService"&gt;
      &lt;property name="maxWidth" value="${scaling.thumbnail.maxWidth}" /&gt;
      &lt;property name="maxHeight" value="${scaling.thumbnail.maxHeight}" /&gt;
  &lt;/bean&gt;
  </code></pre>
  
  <p><strong>I finally found a solution/workaround</strong></p>
  
  <p>It seems like Spring does not like to mix up XML and Java Config or at least it does not work in this case. I tested this with 4.0.9.</p>
  
  <p>Instead of using an XML file in my <code>@ContextConfiguration</code> I referenced a Java Config class that contains a <code>@PropertySource</code> annotation.</p>
  
  <pre><code>@Configuration
  @PropertySource("test.properties")
  @ImportResource("webservices.xml")
  public class TestPlaceholderConfig {
  
  }
  
  @RunWith(SpringJUnit4ClassRunner.class)
  @ContextConfiguration(classes = {TestPlaceholderConfig.class, WebServiceConfig.class})
  public class MyTest {
  }
  </code></pre>
  
  <p>Weird thing is that the webservices.xml also contains a bean definition for the WebServiceConfig class. However, Spring is unable to find the bean defined in the Java Config. Thus I had to add the WebServiceConfig.class to the ContextConfiguration of the test class.</p>