Java 将基于概要文件的属性文件解析为spring测试
在我的springJava 将基于概要文件的属性文件解析为spring测试,java,spring,unit-testing,spring-test,Java,Spring,Unit Testing,Spring Test,在我的springRootConfig类中,我根据我的spring配置文件使用属性文件: @Configuration @PropertySource("classpath:properties/app.properties") @PropertySource("classpath:properties/app-${spring.profiles.active}.properties") @ComponentScan(...) public class RootConfig {
RootConfig
类中,我根据我的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:无法加载ApplicationContext
,因为
无法解析字符串值“classpath:properties/app-${spring.profiles.active}.properties”中的占位符“spring.profiles.active”
这是web应用程序,因此我的spring配置文件最初配置在:
public class WebAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
@Override
public void onStartup(ServletContext servletContext) throws ServletException {
super.onStartup(servletContext);
servletContext.setInitParameter("spring.profiles.active", getSpringProfilesActive());
}
}
getSpringProfilesActive()
-是一个静态方法,它读取系统属性而不依赖于上下文。可能在测试运行期间WebAppInitilizer
尚未启动。在@PropertySource
中可能无法正确计算属性SPEL。您可以尝试在属性资源占位符配置器中确定您的配置文件
@ActiveProfiles("test")
public TestClass {
...
}
@Configuration
public class PropertySourcesConfig {
@Profile("dev")
public static class DevConfig {
@Bean
public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
PropertySourcesPlaceholderConfigurer pspc = new PropertySourcesPlaceholderConfigurer();
pspc.setLocations(new Resources[] {
"app - dev.properties"
});
return pspc;
}
}
@Profile("test")
public static class DevConfig {
@Bean
public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
PropertySourcesPlaceholderConfigurer pspc = new PropertySourcesPlaceholderConfigurer();
pspc.setLocations(new Resources[] {
"app - test.properties"
});
return pspc;
}
}
}
在您的情况下,servletContext.setInitParameter(“spring.profiles.active”,“dev”)
被设置为WebAppInitializer
的一部分,在运行测试用例时不会被调用,请在调用测试之前将spring.profiles.active
设置为dev
,如下所示:
import java.util.Properties;
import org.junit.BeforeClass;
import org.junit.Test;
import static org.junit.Assert.assertTrue;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = RootConfig.class)
public class RootConfigTest {
@BeforeClass
public static void setSystemProperty() {
Properties properties = System.getProperties();
properties.setProperty("spring.profiles.active", "dev");
}
@Test
public void testContext() throws Exception {
assertTrue(true);
}
}
我已经尝试得到相同的异常。doh,我已经尝试了类似的东西,但没有使用@BeforeClass
和static
。谢谢,这个有用。