当前位置: 首页 > 知识库问答 >
问题:

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

赵英哲
2023-03-14

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

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

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

  • 类路径:/test。属性(我的首选符号)
  • /测试。属性
  • 文件:src/test/resources/test.properties

我也确信Spring本身是有效的,因为当我删除@Value时,被测试的类会像预期的那样通过@Autow在我的测试中注入。

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

package my.package.service;

// Imports

@Service
public class MyService {

    @Value("${someFlag:false}")
    private Boolean someFlag;

    public boolean hasFlag() {
        return BooleanUtils.isTrue(someFlag);
    }
}
@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>

共有3个答案

辛麻雀
2023-03-14

除了比茹的回答:

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

@Configuration
static class PropertyConfig {
    @Bean
    public static ConfigurationPropertiesBindingPostProcessor propertiesProcessor() {
        return new ConfigurationPropertiesBindingPostProcessor();
    }
}
齐学文
2023-03-14

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

@TestPropertySource(value="classpath:/config/test.properties")

为了加载特定属性以进行 junit 测试

孔华池
2023-03-14

这里的问题是你需要一个 PropertySourcesPlaceholderConfigurer,它实际上负责解析 ${..} 字段,只需添加另一个创建此 bean 的 bean:

@Bean
public static PropertySourcesPlaceholderConfigurer propertiesResolver() {
    return new PropertySourcesPlaceholderConfigurer();
}
 类似资料:
  • 今天,我将我的项目从Spring Boot1.5.9更新到2.1.1,我的一些测试停止工作。当我开始测试时,控制台会弹出错误: com.example.rest.config.SecurityConfig中的authEntryPoint字段需要一个类型为“com.example.rest.service.auth.EntryPoints.AuthenticationEntryPoint”的bean

  • 通过导出一个接受Karma将要使用的配置对象的函数,可以将配置文件放在一起。 修改此对象的某些属性将告诉Karma我们想要做什么。 让我们来看一下在这个配置文件中使用的一些关键属性: 'jasmine', ], frameworks是我们要使用的测试框架的列表。这些框架必须通过NPM作为依赖项安装在我们的项目中,或/和作为Karma插件。 插件 plugins: [ 'karma-jasm

  • 运行一个maven测试,两个测试都通过了。 这是我的代码: 非常感谢你的帮助

  • 我正在使用SoapUI Pro。SoapUI网站表示,我应该能够在测试执行期间的任何时候设置上下文变量并获取这些值。在帮助中,它说: 常见的使用方案是通过将相应的计数器和集合保存到上下文并使用它们根据需要控制流来循环或跟踪进度。 如果我使用现成的“运行测试用例”测试步骤,我无法让它工作。被调用的测试似乎没有传递上下文信息。 这是一个非常基本的例子。在我最初的测试中,我放了一些Groovy脚本,上面

  • 我有一个方法,如果名称与正则表达式匹配,则必须返回true,如果名称具有特殊字符或数字,则必须返回null。 这是方法: 这是对无法通过的方法的测试:

  • 我在表中为某些列设置了默认值。例如,create_time我已经设置了CURRENT_TIMESTAMP。当我通过JPA插入一个对象时,我没有得到该列的默认值。是否有任何命令可以执行此操作?