我有一个Web应用程序,它需要针对不同环境使用不同的配置文件,其中一些配置被放置在应用服务器上作为JNDI数据源,但是另一些配置则存储在属性文件中。
因此,我想要使用Spring配置文件的特性。
我的问题是我无法让测试用例运行起来。
context.xml:
<context:property-placeholder
location="classpath:META-INF/spring/config_${spring.profiles.active}.properties"/>
测试:
@RunWith(SpringJUnit4ClassRunner.class)
@TestExecutionListeners({
TestPreperationExecutionListener.class
})
@Transactional
@ActiveProfiles(profiles = "localtest")
@ContextConfiguration(locations = {
"classpath:context.xml" })
public class TestContext {
@Test
public void testContext(){
}
}
问题似乎是加载配置文件的变量没有被解析:
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
当前配置文件应使用@ActiveProfile
注释进行设置。由于这是一个测试用例,我将无法使用web.xml
。如果可能的话,我想避免运行时选项。测试应该按原样运行(如果可能的话)。
我如何正确激活配置文件?可以使用context.xml设置配置文件吗?我能否在调用普通上下文的测试上下文中声明变量?