将基于配置文件的属性文件解析为弹簧测试

时间:2016-09-08 07:56:00

标签: java spring unit-testing spring-test

在我的春季RootConfig班级中,我根据我的春季档案使用属性文件:

@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"

这是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() - 是一个静态方法,它读取System属性而不依赖于上下文。

2 个答案:

答案 0 :(得分:2)

在您的情况下,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); } } 测试,如下:

UITableView

答案 1 :(得分:0)

可能在测试期间,WebAppInitilizer运行尚未开始。在@PropertySource内可能无法正确评估属性SPEL。您可以尝试在PropertySourcesPlaceholderConfigurer内确定自己的个人资料。

 @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;
  }
 }

}