我有一个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设置配置文件? 我可以在实际调用正常上下文的test-context.xml中声明变量吗?
感谢。下面添加了一项改进。 –
此方法不干净,您将不得不转到活动配置文件值应更改的属性文件。 – codebusta