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

Resolve profile based properties file into spring tests

进入我的 spring RootConfig class 我使用基于我的 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();
    }

}

现在我想编写测试 class 使用这样的配置:

@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() - 是一个静态方法,读取系统 属性 并且不依赖于上下文。

可能在您的测试期间 运行 WebAppInitilizer 尚未开始。 属性 SPEL 可能无法在 @PropertySource 中正确评估。您可以尝试在 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;
  }
 }

}

在你的例子中,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);
    }

}