@TestPropertySource无法正常工作?

时间:2016-11-10 18:35:52

标签: spring spring-annotations spring-test spring-java-config

我正在尝试测试我的Spring配置。这是我的配置类

@Configuration
@PropertySource("/etc/my.properties")
@ComponentScan("my.package")
public class SpringConfig {

    @Bean
    .....
}

当我试图通过我的测试来测试它时

@RunWith(SpringJUnit4ClassRunner.class)
@TestPropertySource(locations = "classpath:test.properties")
@ContextConfiguration(classes = {SpringConfigIntTest.class, SpringConfig.class})
public class SpringConfigIntTest {
    .......
}

我不断发现无法找到/etc/my.properties的失败。但是我认为我已经用test.properties覆盖了它,我一直在谷歌上搜索而没有看到其他人以不同的方式做到这一点。

我使用的是spring-context,spring-beans,spring-core 4.2.2和spring-test 4.2.4。有人可以在这里给我一些帮助吗?深深体会到它

1 个答案:

答案 0 :(得分:2)

如果资源不存在,您可以将属性源设置为不失败:

@PropertySource(value = "/etc/my.properties", ignoreResourceNotFound = true)