【问题标题】:@TestPropertySource is not loading properties@TestPropertySource 没有加载属性
【发布时间】:2016-01-27 02:16:10
【问题描述】:

我正在为我的 Spring Boot 应用程序编写集成测试,但是当我尝试使用 @TestPropertySource 覆盖某些属性时,它会加载上下文 xml 中定义的属性文件,但它不会覆盖注释中定义的属性。

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = {DefaultApp.class, MessageITCase.Config.class})
@WebAppConfiguration
@TestPropertySource(properties = {"spring.profiles.active=hornetq", "test.url=http://www.test.com/",
                    "test.api.key=343krqmekrfdaskfnajk"})
public class MessageITCase {
    @Value("${test.url}")
    private String testUrl;

    @Value("${test.api.key}")
    private String testApiKey;

    @Test
    public void testUrl() throws Exception {
        System.out.println("Loaded test url:" + testUrl);
    }



    @Configuration
    @ImportResource("classpath:/META-INF/spring/test-context.xml")
    public static class Config {

    }
}

【问题讨论】:

  • 您解决了这个问题吗?通过 TestPropertySource 的内联属性似乎也不适合我。
  • 还没有,但是我已经更改了我的配置,以便我使用@IntegrationTest 注释而不是@TestPropertySource。我会尽快发布答案。

标签: spring spring-boot integration-testing spring-test


【解决方案1】:

我用 Spring Boot 1.4 测试了这个特性 下面的行效果很好

@TestPropertySource(properties = { "key=value", "eureka.client.enabled=false" })

尽管如此,新的 @SpringBootTest 注释也可以正常工作

@RunWith(SpringRunner.class)
@SpringBootTest(
    webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT,
    properties = { "key=value", "eureka.client.enabled=false" }
)
public class NewBootTest {

    @Value("${key}")
    public String key;

    @Test
    public void test() {
        System.out.println("great " + key);
    }
}

【讨论】:

    【解决方案2】:

    我遇到了类似的问题。我通过更新 Spring Context beans.xml 来修复它 org.springframework.context.support.PropertySourcesPlaceholderConfigurer 而不是 org.springframework.beans.factory.config.PropertyPlaceholderConfigurer

    来自 PropertyPlaceholderConfigurer 的 JavaDoc

    已弃用。 从 5.2 开始;改用org.springframework.context.support.PropertySourcesPlaceholderConfigurer,通过利用EnvironmentPropertySource 机制更灵活。

    【讨论】:

      猜你喜欢
      • 2016-02-24
      • 2017-01-04
      • 2018-03-24
      • 2012-02-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-28
      • 1970-01-01
      相关资源
      最近更新 更多