@TestPropertySource 未加载属性

kos*_*ker 5 spring integration-testing spring-test spring-boot

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

    }
}
Run Code Online (Sandbox Code Playgroud)

小智 3

我用 Spring Boot 1.4 测试了这个功能,下面的行效果很好

@TestPropertySource(properties = { "key=value", "eureka.client.enabled=false" })
Run Code Online (Sandbox Code Playgroud)

尽管如此,新的 @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);
    }
}
Run Code Online (Sandbox Code Playgroud)