【发布时间】:2015-12-14 12:26:02
【问题描述】:
似乎我在 Spring 4.1.17 中使用 Spring Boot 1.2.6.RELEASE 所做的任何事情都不起作用。我只想访问应用程序属性并在必要时用测试覆盖它们(不使用黑客手动注入 PropertySource)
这不起作用..
@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 之间似乎有很多相互矛盾的信息,我找不到任何可以确定的东西。
任何见解将不胜感激。干杯!
【问题讨论】:
-
TestPropertySource工作。 -
TestPropertySource 不会根据docs.spring.io/spring-boot/docs/current/reference/html/… 覆盖环境变量
-
Spring boot 提供了注解@SpringBootTest,可以用来读取配置yml。详情请看这里:stackoverflow.com/a/50309219/1169093
标签: java spring junit spring-boot