我有一个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的配置文件? 我可以声明变量,实际上是调用正常情况下一个测试的context.xml?
我可以建议这样做,定义你的测试是这样的:
@RunWith(SpringJUnit4ClassRunner.class)
@TestExecutionListeners({
TestPreperationExecutionListener.class
})
@Transactional
@ActiveProfiles(profiles = "localtest")
@ContextConfiguration
public class TestContext {
@Test
public void testContext(){
}
@Configuration
@PropertySource("classpath:/myprops.properties")
@ImportResource({"classpath:context.xml" })
public static class MyContextConfiguration{
}
}
在myprops.properties以下内容的文件:
spring.profiles.active=localtest
有了这个你的第二个属性文件应该得到解决:
META-INF/spring/config_${spring.profiles.active}.properties
综观六必居的答案,我找到了一个工作方案。
我创建了一个额外的上下文文件test-context.xml
:
<context:property-placeholder location="classpath:config/spring-test.properties"/>
含有个人资料:
spring.profiles.active=localtest
和加载试验用:
@RunWith(SpringJUnit4ClassRunner.class)
@TestExecutionListeners({
TestPreperationExecutionListener.class
})
@Transactional
@ActiveProfiles(profiles = "localtest")
@ContextConfiguration(locations = {
"classpath:config/test-context.xml" })
public class TestContext {
@Test
public void testContext(){
}
}
创建多个测试案例时,这样可以节省一些工作。
这里最好的办法是消除@ActiveProfiles注释并执行以下操作:
@RunWith(SpringJUnit4ClassRunner.class)
@TestExecutionListeners({
TestPreperationExecutionListener.class
})
@Transactional
@ContextConfiguration(locations = {
"classpath:config/test-context.xml" })
public class TestContext {
@BeforeClass
public static void setSystemProperty() {
Properties properties = System.getProperties();
properties.setProperty("spring.profiles.active", "localtest");
}
@AfterClass
public static void unsetSystemProperty() {
System.clearProperty("spring.profiles.active");
}
@Test
public void testContext(){
}
}
和你的测试context.xml中应具备以下条件:
<context:property-placeholder
location="classpath:META-INF/spring/config_${spring.profiles.active}.properties"/>
public class LoginTest extends BaseTest {
@Test
public void exampleTest( ){
// Test
}
}
从基测试类继承(本示例中是testng
而非jUnit
,但ActiveProfiles
是相同的):
@ContextConfiguration(locations = { "classpath:spring-test-config.xml" })
@ActiveProfiles(resolver = MyActiveProfileResolver.class)
public class BaseTest extends AbstractTestNGSpringContextTests { }
MyActiveProfileResolver
可以包含以确定要使用哪个配置文件所需的任何逻辑:
public class MyActiveProfileResolver implements ActiveProfilesResolver {
@Override
public String[] resolve(Class<?> aClass) {
// This can contain any custom logic to determine which profiles to use
return new String[] { "exampleProfile" };
}
}
这台,然后将其用于解决通过测试所需依赖的配置文件。