相关疑难解决方法(0)

@TestPropertySource在Spring 1.2.6中使用AnnotationConfigContextLoader进行JUnit测试不起作用

看起来我在Spring 4.1.1中使用Spring Boot 1.2.6.RELEASE做的事情根本不起作用.我只是想访问应用程序属性并在必要时使用测试覆盖它们(不使用hack手动注入PropertySource)

这不起作用..

@TestPropertySource(properties = {"elastic.index=test_index"})
Run Code Online (Sandbox Code Playgroud)

这也不是..

@TestPropertySource(locations = "/classpath:document.properties")
Run Code Online (Sandbox Code Playgroud)

也不是..

@PropertySource("classpath:/document.properties")
Run Code Online (Sandbox Code Playgroud)

完整测试案例..

@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);
    }
}
Run Code Online (Sandbox Code Playgroud)

导致

org.junit.ComparisonFailure: 
Expected :test_index
Actual   :${elastic.index}
Run Code Online (Sandbox Code Playgroud)

似乎3.x和4.x之间存在很多相互矛盾的信息,我找不到任何可行的信息.

任何见解将不胜感激.干杯!

java junit spring spring-boot

26
推荐指数
5
解决办法
7万
查看次数

标签 统计

java ×1

junit ×1

spring ×1

spring-boot ×1