【问题标题】:Resolve profile based properties file into spring tests将基于配置文件的属性文件解析为弹簧测试
【发布时间】: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


    【解决方案1】:

    在您的情况下,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,我已经尝试过类似的东西,但不是 @BeforeClassstatic。谢谢,这行得通。
    【解决方案2】:

    可能在您的测试期间运行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;
      }
     }
    
    }
    

    【讨论】:

    • 我已经尝试得到同样的异常。
    猜你喜欢
    • 1970-01-01
    • 2012-11-02
    • 1970-01-01
    • 2021-08-16
    • 2017-05-06
    • 1970-01-01
    • 1970-01-01
    • 2011-10-23
    相关资源
    最近更新 更多