spring – @TestPropertySource没有加载属性

我正在为我的
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 {

    }
}

最佳答案 我用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);
    }
}
点赞