@TestPropertySource未加载属性

时间:2015-10-27 15:21:24

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

我正在为我的spring启动应用程序编写集成测试,但是当我尝试使用@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 {

    }
}

2 个答案:

答案 0 :(得分:2)

我使用Spring Boot 1.4测试了这个功能 下面的线很好用

SUBROUTINE lu_d(aa,l,m)
implicit none
real,intent(in):: a(:,:)
real,allocatable,intent(out):: l(:,:), m(:,:)
integer:: i,j,k,size_a1,size_a2
real:: s

size_a1=size(aa,1)
size_a2=size(aa,2)
allocate( l(size_a1,size_a2), m(size_a1,size_a2))

<operations>

RETURN
END SUBROUTINE lud_d

然而,新的@SpringBootTest注释也正常工作

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

答案 1 :(得分:1)

我有类似的问题。我通过更新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机制更加灵活。