Spring @TestPropertySource未加载属性
我正在为我的spring boot应用程序编写集成测试,但当我尝试使用@TestPropertySource覆盖某些属性时,它正在加载上下文xml中定义的属性文件,但它没有覆盖注释中定义的属性Spring @TestPropertySource未加载属性,spring,spring-boot,integration-testing,spring-test,Spring,Spring Boot,Integration Testing,Spring Test,我正在为我的spring boot应用程序编写集成测试,但当我尝试使用@TestPropertySource覆盖某些属性时,它正在加载上下文xml中定义的属性文件,但它没有覆盖注释中定义的属性 @RunWith(SpringJUnit4ClassRunner.class) @SpringApplicationConfiguration(classes = {DefaultApp.class, MessageITCase.Config.class}) @WebAppConfiguration @T
@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 {
}
}
我用SpringBoot1.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);
}
}
我也有类似的问题。我通过更新Spring上下文bean.xml来修复它
org.springframework.context.support.propertysourcesplaceplaceplaceconfigurer
而不是org.springframework.beans.factory.config.PropertyPlaceholderConfigurer
从PropertyPlaceHolderConfigure的JavaDoc
不赞成。
截至5.2;通过利用和机制,使用更灵活的
你解决这个问题了吗?通过TestPropertySource的内联属性似乎对我也不起作用。还没有,但我已经更改了配置,所以我使用了
@IntegrationTest
注释,而不是@TestPropertySource
。我很快会给你回复的。