I just configured a property placeholder in my Spring configuration
<context:property-placeholder location="classpath:/config/config.properties" />
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 ApplicationContext
because of a FileNotFoundException
. This happens if I try to run the tests from Eclipse as well as when running the test via maven.
I also tried to configure the PropertyPlaceholderConfigurer
directly with the same result.
It seems as the file is not in the classpath location, even though the test classes are annotated with
@ContextConfiguration("classpath:/config/spring-config.xml")
the files are in the same folder and it finds the xml configuration.
I already tried to use different paths: classpath:config/config.properties
and without the classpath prefix, all not working. An absolute path with the file prefix works, but thats not a good solution.
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?
My test classes look kind of like this:
@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);
}
and the context xml looks like this:
<context:property-placeholder location="classpath:/config/config.properties" />
<bean id="imageScalingService" class="service.image.ImageScalingService">
<property name="maxWidth" value="${scaling.thumbnail.maxWidth}" />
<property name="maxHeight" value="${scaling.thumbnail.maxHeight}" />
</bean>
I finally found a solution/workaround
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.
Instead of using an XML file in my @ContextConfiguration
I referenced a Java Config class that contains a @PropertySource
annotation.
@Configuration
@PropertySource("test.properties")
@ImportResource("webservices.xml")
public class TestPlaceholderConfig {
}
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = {TestPlaceholderConfig.class, WebServiceConfig.class})
public class MyTest {
}
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.
I think if it is a maven project then properties file should be in src/test/resource/config folder. Because while running the test cases the class path for tests is src/test/resource/config. Try to put the config file in test case class path
In which folder is the config.properties located? If you are following the standard maven folder structure it should be in src/main/resources/config/config.properties
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With