Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/305.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
@未通过Java配置的测试上下文设置值_Java_Spring_Testing_Properties_Junit - Fatal编程技术网

@未通过Java配置的测试上下文设置值

@未通过Java配置的测试上下文设置值,java,spring,testing,properties,junit,Java,Spring,Testing,Properties,Junit,我有一个Maven项目,它使用Java配置的Spring(@Configuration等)。@Value引用的属性存储在不同的位置,例如Tomcat的context.xml 为了进行测试,我创建了一个.properties文件,为组件和服务提供一些值。在我的JUnit测试(使用spring测试上下文)中,这个.properties文件是通过@PropertySource添加的。问题是不会从文件中加载值,而是将值标识符设置为值,例如,${someFlag:false}(因此我得到除字符串以外的任何

我有一个Maven项目,它使用Java配置的Spring(
@Configuration
等)。
@Value
引用的属性存储在不同的位置,例如Tomcat的context.xml

为了进行测试,我创建了一个.properties文件,为组件和服务提供一些值。在我的JUnit测试(使用spring测试上下文)中,这个.properties文件是通过
@PropertySource
添加的。问题是不会从文件中加载值,而是将值标识符设置为值,例如,
${someFlag:false}
(因此我得到除字符串以外的任何值的ClassCastException)。而且默认值不会被设置,所以我认为,这些值根本不会被处理

我确信Spring会找到这个文件,因为当我更改
@PropertySource
的值时,我会得到一些FileNotFoundException。尽管如此,我还是尝试了不同的变体来指向这个文件,所有这些都有效(通过重命名产生FileNotFoundException进行测试):

  • classpath:/test.properties(我的首选符号)
  • /测试属性
  • 文件:src/test/resources/test.properties
我还确信Spring本身是有效的,因为当我删除
@值时,测试中的类会按照预期通过
@Autowired
在我的测试中被注入

在下面,你会发现问题场景被尽可能地简化了。有关版本和依赖项,请参见底部的pom.xml

MyService.java MyConfiguration.java MyServiceComponentTest.java MyTestConfiguration.java src/test/resources/test.properties pom.xml

UTF-8
3.2.3.1发布
org.apache.commons
commons-lang3
3.1
org.springframework
弹簧芯
${spring.version}
org.springframework
spring上下文
${spring.version}
org.springframework
弹簧试验
${spring.version}
测试
org.hamcrest
汉克雷斯特图书馆
1.3
测试
朱尼特
朱尼特
4.11
测试

这里的问题是您需要一个
属性资源占位符配置器
也负责解析
${..}
字段,只需添加另一个创建此bean的bean:

@Bean
public static PropertySourcesPlaceholderConfigurer propertiesResolver() {
    return new PropertySourcesPlaceholderConfigurer();
}

使用Spring 4,现在可以使用:


为了加载junit测试的特定属性,除了Biju Kunjummen回答:

如果使用@ConfigurationProperties将属性注入bean setter,则需要创建ConfigurationPropertiesBindingPostProcessor(而不是PropertySourcesPlaceholderConfigurer):


有关
PropertySource
,请参阅和@Value annotations中的
解析${…}占位符
,也请参阅答案。
propertysourcesplaceplaceconfigurer
是一个
BeanFactoryPostProcessor
。在某些情况下,如果调用
propertiesResolver()
后Spring生成一些bean,它可能会工作,但在其他情况下会失败。要确保它正常工作,您需要将其设置为
静态
。您可以扩展您的评论吗?我已经测试了
@TestPropertySource
以及
@Value
,并且没有加载属性。。。
@Configuration
@ComponentScan(basePackages = {"my.package.service"})
public class MyConfiguration {
}
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = {MyTestConfiguration.class})
public class MyServiceComponentTest {

    @Autowired
    private MyService service;

    @Test
    public void hasFlagReturnsTrue() {
        assertThat(service.hasFlag(), is(true));
    }
}
@Configuration
@Import({MyConfiguration.class})
@PropertySource("classpath:/test.properties")
public class MyTestConfiguration {
}
someFlag=true
<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <spring.version>3.2.3.RELEASE</spring.version>
</properties>
<dependencies>
    <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-lang3</artifactId>
        <version>3.1</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>${spring.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>${spring.version}</version>
    </dependency>
    <!-- Test dependencies -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-test</artifactId>
        <version>${spring.version}</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.hamcrest</groupId>
        <artifactId>hamcrest-library</artifactId>
        <version>1.3</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.11</version>
        <scope>test</scope>
    </dependency>
</dependencies>
@Bean
public static PropertySourcesPlaceholderConfigurer propertiesResolver() {
    return new PropertySourcesPlaceholderConfigurer();
}
@TestPropertySource(value="classpath:/config/test.properties")
@Configuration
static class PropertyConfig {
    @Bean
    public static ConfigurationPropertiesBindingPostProcessor propertiesProcessor() {
        return new ConfigurationPropertiesBindingPostProcessor();
    }
}