Java @TestPropertySource没有';在Spring 1.2.6中,JUnit测试无法使用AnnotationConfigContextLoader

Java @TestPropertySource没有';在Spring 1.2.6中,JUnit测试无法使用AnnotationConfigContextLoader,java,spring,junit,spring-boot,Java,Spring,Junit,Spring Boot,我在Spring4.1.17中使用SpringBoot1.2.6.RELEASE所做的任何事情似乎都不起作用。我只想访问应用程序属性,并在必要时使用test覆盖它们(而不使用hack手动注入PropertySource) 这不管用 @TestPropertySource(properties = {"elastic.index=test_index"}) 这也不是 @TestPropertySource(locations = "/classpath:document.properties")

我在Spring4.1.17中使用SpringBoot1.2.6.RELEASE所做的任何事情似乎都不起作用。我只想访问应用程序属性,并在必要时使用test覆盖它们(而不使用hack手动注入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之间有很多相互矛盾的信息,我找不到任何可以肯定有效的信息


如有任何见解,将不胜感激。干杯

您是否尝试过使用
@PropertySource(“classpath:document.properties”)
@PropertySource(“classpath*:document.properties”)

使用@Value需要propertySourcesplaceplaceConfigurer bean来解析
${…}
占位符。请参见此处接受的答案:

证明最好的方法(直到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 {
    }
}

我使用
@TestPropertySource
locations
属性覆盖(或添加)属性

这对我很有用(spring 4.2.4):

但覆盖下面这样的属性并没有:

@TestPropertySource(
  locations = {"classpath:test.properties"},
  properties = { "key=value" })
尽管javadoc说这些属性具有最高的优先级。也许是虫子

更新

该错误应该在Spring boot 1.4.0及更高版本中修复。看。
到目前为止,以所述方式声明的属性应该获得优先权。

对于我来说@TestPropertySource(“classpath:xxxxxxxx.properties”)起作用了

我对@TestPropertySource有意见。找不到test.properties

下面是修复前的一个

@RunWith(SpringRunner.class)
@ContextConfiguration(classes = ExtContext.class)
@TestPropertySource(locations = "classpath: test.properties")
我删除了classpath:和test.properties之间的空格,如下所示

@RunWith(SpringRunner.class)
@ContextConfiguration(classes = ExtContext.class)
@TestPropertySource(locations = "classpath:test.properties")

当在classpth中找不到test.properties时,这对我很有用。misht也适用于您

如果您遇到此问题,并且正在尝试将yaml用作属性文件,请记住spring
@TestPropertySource
@PropertySource
不适用于yaml文件,并且不会正确加载属性


两种方法都试过了。他们在4.x中引入了@TestPropertySource,尽管它看起来有缺陷。我记得我在集成测试中遇到了同样的问题。如果他们还没有解决,我希望他们能尽快解决。@ThomasBeauvais您有问题id的参考资料吗?我想追踪他们是否解决了问题,问题是为什么?这似乎是一个巨大的疏忽。为什么这个功能消失了?无论如何如果这是唯一的办法。。那就这样吧。。虽然@TestPropertySource应该可以工作。@Don Bottstein:请您突出显示PropertySourcesplacePlaceholderConfigurer的源部分,因为我想很多像我这样的人都忽略了这一部分,因为他们已经有了可用的PropertyPlaceholderConfigurer,这还不够。确实看起来像个bug:
TestPropertySource
works.TestPropertySource没有覆盖环境变量,根据Spring boot提供的注释@SpringBootTest,它可以用来读取配置yml。请检查此处以了解详细信息:您真正需要的只是在应用程序上下文中使用一个
PropertySourcesPlaceholderConfigurer
,然后
@PropertySource
将起作用。您可以在
@Configuration
类中执行此操作(此处的格式设置不起作用):public@Bean PropertySourcesPlaceholderConfigurer PropertySourcesPlaceholderConfigurer(){返回新的PropertySourcesPlaceholderConfigurer();}
@RunWith(SpringRunner.class)
@ContextConfiguration(classes = ExtContext.class)
@TestPropertySource(locations = "classpath:test.properties")