java - @TestPropertySource 不适用于 Spring 1.2.6 中使用 AnnotationConfigContextLoader 的 JUnit 测试

标签 java spring junit spring-boot

似乎我在 Spring 4.1.17 中使用 Spring Boot 1.2.6.RELEASE 所做的任何事情都不起作用。我只想访问应用程序属性并在必要时用测试覆盖它们(不使用黑客手动注入(inject) 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 之间似乎有很多相互矛盾的信息,我找不到任何可以确定的东西。

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

最佳答案

事实证明,最好的方法(直到 Spring 修复这个疏忽)是一个 PropertySourcesPlaceholderConfigurer,它将引入 test.properties(或任何你想要的)和 @Import 或扩展那个 @Configuration

import org.apache.commons.lang3.ArrayUtils;
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;

import java.io.IOException;

@Configuration
public class PropertyTestConfiguration {
    @Bean
    public PropertyPlaceholderConfigurer propertyPlaceholderConfigurer() throws IOException {
        final PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer();
        ppc.setLocations(ArrayUtils.addAll(
                        new PathMatchingResourcePatternResolver().getResources("classpath*:application.properties"),
                        new PathMatchingResourcePatternResolver().getResources("classpath*:test.properties")
                )
        );

        return ppc;
    }

}

这允许您在 application.properties 中定义默认值并在 test.properties 中覆盖它们。当然,如果你有多个方案,那么你可以根据需要配置 PropertyTestConfiguration 类。

并在单元测试中使用它。

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(loader = AnnotationConfigContextLoader.class)
public class PropertyTests {
    @Value("${elastic.index}")
    String index;

    @Configuration
    @Import({PropertyTestConfiguration.class})
    static class ContextConfiguration {
    }
}

关于java - @TestPropertySource 不适用于 Spring 1.2.6 中使用 AnnotationConfigContextLoader 的 JUnit 测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32633638/

相关文章:

java - jUnit单元测试空指针异常而不是自定义异常

unit-testing - 模拟使用 findAll 的 grails 方法,生成 MissingMethodException

java - 从链表中删除所有节点(给定开始和结束索引)

java - 如何在android中以Json格式存储日志数据?

java.lang.IllegalStateException : Cannot load driver class: com. microsoft.sqlserver.jdbc.SQLServerDriver

java - 如何在考虑DDD的基础上构建一个简单的REST应用( Spring / hibernate )?

java - JaxWsPortProxyFactoryBean 查询超时

java - 列出已运行的 jUnit 测试

java - class is-a-JPanel 当它应该有-a

java - 删除文本中的一行;方法不起作用