【发布时间】:2016-09-08 07:55:59
【问题描述】:
在我的 springRootConfig 类中,我使用基于我的 spring 配置文件的属性文件:
@Configuration
@PropertySource("classpath:properties/app.properties")
@PropertySource("classpath:properties/app-${spring.profiles.active}.properties")
@ComponentScan(...)
public class RootConfig {
@Bean // just because you will ask about it
public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
return new PropertySourcesPlaceholderConfigurer();
}
}
现在我想编写使用这种配置的测试类:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = RootConfig.class)
public class RootConfigTest {
@Test
public void testContext() throws Exception {
assertTrue(true);
}
}
但我的上下文无法启动:java.lang.IllegalStateException: Failed to load ApplicationContext 因为
Could not resolve placeholder 'spring.profiles.active' in string value "classpath:properties/app-${spring.profiles.active}.properties"
这是网络应用程序,所以最初我的 spring 配置文件配置在:
public class WebAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
@Override
public void onStartup(ServletContext servletContext) throws ServletException {
super.onStartup(servletContext);
servletContext.setInitParameter("spring.profiles.active", getSpringProfilesActive());
}
}
getSpringProfilesActive() - 是一个静态方法,它读取系统属性而不依赖于上下文。
【问题讨论】:
标签: java spring unit-testing spring-test