问题描述
我在 Spring 4.1.17 中使用 Spring Boot 1.2.6.RELEASE 所做的任何事情似乎都不起作用.我只想访问应用程序属性并在必要时用测试覆盖它们(不使用 hack 手动注入 PropertySource)
It doesn't seem that anything I do in Spring 4.1.17 with Spring Boot 1.2.6.RELEASE works at all. I just want to access the application properties and override them with test if necessary (without using the hack to inject a PropertySource manually)
这不起作用..
@TestPropertySource(properties = {"elastic.index=test_index"})
这也不是..
@TestPropertySource(locations = "/classpath:document.properties")
也不是这个..
@PropertySource("classpath:/document.properties")
完整的测试用例..
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(loader = AnnotationConfigContextLoader.class)
@TestPropertySource(properties = {"elastic.index=test_index"})
public class PropertyTests {
@Value("${elastic.index}")
String index;
@Configuration
@TestPropertySource(properties = {"elastic.index=test_index"})
static class ContextConfiguration {
}
@Test
public void wtf() {
assertEquals("test_index", index);
}
}
导致
org.junit.ComparisonFailure:
Expected :test_index
Actual :${elastic.index}
似乎 3.x 和 4.x 之间有很多相互矛盾的信息,我找不到任何可以肯定的内容.
It seems there is a lot of conflicting information between 3.x and 4.x and I can't find anything that will work for sure.
任何见解将不胜感激.干杯!
Any insight would be gratefully appreciated. Cheers!
推荐答案
您对 @Value 的使用需要一个 PropertySourcesPlaceholderConfigurer bean 来解析 ${...}
占位符.请参阅此处接受的答案:@Value not set via Java-configured test context
Your use of @Value requires a PropertySourcesPlaceholderConfigurer bean to resolve ${...}
placeholders. See the accepted answer here: @Value not set via Java-configured test context
这篇关于@TestPropertySource 不适用于 Spring 1.2.6 中使用 AnnotationConfigContextLoader 的 JUnit 测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!