0

0

Spring配置类与外部属性单元测试实践指南

碧海醫心

碧海醫心

发布时间:2025-09-15 14:05:01

|

611人浏览过

|

来源于php中文网

原创

Spring配置类与外部属性单元测试实践指南

本文深入探讨了在Spring框架中,如何为依赖外部配置属性的@Configuration类编写有效的单元测试。针对属性无法正确注入导致测试失败的常见问题,教程提供了多种解决方案,包括使用@PropertySource、理解@DependsOn的适用性,以及Spring Boot中@ConfigurationPropertiesScan的现代实践,旨在帮助开发者构建健壮的配置测试。

引言:配置类单元测试的挑战

在spring应用中,@configuration类是定义和配置bean的核心。当这些配置bean依赖于通过@configurationproperties注解绑定的外部属性时,编写单元测试可能会遇到挑战。一个常见的场景是,尽管在测试环境中指定了属性文件,但@configurationproperties对象中的属性值仍然为空,导致bean创建失败。本文将基于一个具体的jms连接配置示例,深入探讨这一问题的原因及多种解决方案。

示例配置类概览

假设我们有一个用于JMS消息网关连接的配置类JmsMessageGatewayConnectionConfig,它依赖于JmsMessageGatewayProperties来获取连接参数:

@Configuration
public class JmsMessageGatewayConnectionConfig {

    @Bean
    public JmsMessageGatewayConnection jmsMessageGatewayConnection (final JmsMessageGatewayProperties jmsConfig) throws JMSException {
        // 使用jmsConfig和cachingConnectionFactory创建JmsMessageGatewayConnection
        return new JmsMessageGatewayConnection(jmsConfig, cachingConnectionFactory(jmsConfig));
    }

    private CachingConnectionFactory cachingConnectionFactory(final JmsMessageGatewayProperties jmsConfig) {
        CachingConnectionFactory cachingConnectionFactory = new CachingConnectionFactory();
        cachingConnectionFactory.setTargetConnectionFactory(jmsConnectionFactory(jmsConfig));
        cachingConnectionFactory.resetConnection();
        return cachingConnectionFactory;
    }

    private JmsConnectionFactory jmsConnectionFactory(final JmsMessageGatewayProperties jmsConfig) {
        JmsConnectionFactory jmsConnectionFactory =
                new JmsConnectionFactory(jmsConfig.getUsername(), jmsConfig.getPassword(), jmsConfig.getRemoteUri());
        jmsConnectionFactory.setReceiveLocalOnly(true);
        return jmsConnectionFactory;
    }

    @Bean
    @ConfigurationProperties(prefix = "jms")
    public JmsMessageGatewayProperties messageGatewayProperties() {
        return new JmsMessageGatewayProperties();
    }
}

以及对应的属性类:

public class JmsMessageGatewayProperties {
    private String remoteUri;
    private String username;
    private String password;
    private boolean messagePersistent;
    private Integer forceDetachedRetryLimit = 1;

    // Getter和Setter方法省略
    public String getRemoteUri() { return remoteUri; }
    public void setRemoteUri(final String remoteUri) { this.remoteUri = remoteUri; }
    // ... 其他属性的Getter/Setter
}

我们的目标是测试JmsMessageGatewayConnection这个Bean是否能被Spring上下文正确地创建和注入。

初始测试尝试与问题分析

为了测试上述配置,我们通常会使用Spring的测试支持:

@RunWith(SpringRunner.class)
@ContextConfiguration(classes = { JmsMessageGatewayConnectionConfig.class})
@TestPropertySource(locations = "classpath:camel.properties")
public class JmsMessageGatewayConnectionConfigTest {

    @Autowired
    private JmsMessageGatewayConnection jmsMessageGatewayConnection;

    @Test
    public void jmsMessageGatewayConnectionConfigTest() {
        Assert.assertNotNull(jmsMessageGatewayConnection);
    }
}

并且在camel.properties文件中定义了jms前缀的属性:

jms.remoteUri=vm://localhost:61616
jms.username=username
jms.password=password

然而,上述测试会失败,并抛出Invalid URI: cannot be null or empty的错误。通过调试发现,JmsMessageGatewayProperties对象虽然被创建了,但其内部的remoteUri、username、password等属性值却为null。

问题根源在于,尽管@TestPropertySource加载了属性文件,但Spring容器在处理@ConfigurationProperties时,并没有自动将这些属性绑定到JmsMessageGatewayProperties实例上。@TestPropertySource主要负责将属性加载到Spring的Environment中,但@ConfigurationProperties的绑定机制还需要额外的配置来“激活”它。

解决方案

为了确保@ConfigurationProperties能够正确地从测试属性源中加载值,我们有以下几种主要策略:

方案一:在@ConfigurationProperties类或其配置上使用@PropertySource

最直接的解决方案是在JmsMessageGatewayProperties类上添加@PropertySource注解,或者在定义@ConfigurationProperties Bean的方法上添加。这明确告诉Spring从哪个文件加载这些属性。

推荐做法:在JmsMessageGatewayProperties类上添加@PropertySource

// JmsMessageGatewayProperties.java
@PropertySource("classpath:camel.properties") // 添加此行
public class JmsMessageGatewayProperties {
    private String remoteUri;
    private String username;
    private String password;
    // ... 其他属性及Getter/Setter
}

通过这种方式,JmsMessageGatewayProperties类自身就携带着加载其属性的指令。当Spring容器扫描到这个类并尝试绑定jms前缀的属性时,它会知道去camel.properties中查找。

或者:在@Configuration类中通过@PropertySource加载

Solvely
Solvely

AI学习伴侣,数学解体,作业助手,家教辅导

下载

如果不想修改JmsMessageGatewayProperties类本身,也可以在JmsMessageGatewayConnectionConfig中加载属性:

@Configuration
@PropertySource("classpath:camel.properties") // 添加此行,确保属性被加载到Environment中
public class JmsMessageGatewayConnectionConfig {
    // ... 保持原有代码不变
    @Bean
    @ConfigurationProperties(prefix = "jms")
    public JmsMessageGatewayProperties messageGatewayProperties() {
        return new JmsMessageGatewayProperties();
    }
}

这种方式确保了camel.properties中的属性在JmsMessageGatewayConnectionConfig被处理时已经存在于Spring的Environment中,从而允许@ConfigurationProperties正确绑定。

更新后的测试类(无需修改) 无论选择哪种@PropertySource的放置方式,原始的测试类通常无需修改,因为它已经通过@TestPropertySource将属性加载到测试环境的Environment中。但为了确保@ConfigurationProperties的绑定机制能够感知到这些属性,上述@PropertySource的添加是关键。

方案二:利用Spring Boot的@ConfigurationPropertiesScan

如果你正在使用Spring Boot,并且版本较新(Spring Boot 2.2+),可以使用@ConfigurationPropertiesScan来简化@ConfigurationProperties的注册和绑定过程。

首先,JmsMessageGatewayProperties类不再需要@PropertySource注解,但需要确保它是一个Bean或者被Spring组件扫描到:

// JmsMessageGatewayProperties.java
// 移除 @PropertySource
// 可以添加 @Component 或通过 @EnableConfigurationProperties 注册
public class JmsMessageGatewayProperties {
    // ... 属性及Getter/Setter
}

然后,在你的主应用类或测试配置类上添加@ConfigurationPropertiesScan:

// 例如,在测试配置类上
@RunWith(SpringRunner.class)
@ContextConfiguration(classes = { JmsMessageGatewayConnectionConfig.class})
@TestPropertySource(locations = "classpath:camel.properties")
@ConfigurationPropertiesScan // 添加此行
public class JmsMessageGatewayConnectionConfigTest {
    // ... 测试代码
}

或者,如果JmsMessageGatewayProperties是通过@EnableConfigurationProperties注册的,@ConfigurationPropertiesScan会扫描到它:

@Configuration
@EnableConfigurationProperties(JmsMessageGatewayProperties.class) // 显式注册
public class JmsMessageGatewayConnectionConfig {
    // ...
    // 可以移除 @Bean 和 @ConfigurationProperties(prefix = "jms") 的方法,
    // 因为 @EnableConfigurationProperties 会自动处理
}

@ConfigurationPropertiesScan会自动扫描带有@ConfigurationProperties注解的类,并将其注册为Spring Bean,同时尝试绑定属性。这在Spring Boot应用中是推荐的现代化做法。

方案三:理解@DependsOn(通常不适用于属性绑定)

在原始问题中提到了@DependsOn,它允许你定义一个Bean在另一个Bean被创建之后才被创建。

@Bean
@DependsOn("messageGatewayProperties") // 假设messageGatewayProperties是JmsMessageGatewayProperties Bean的名称
public JmsMessageGatewayConnection jmsMessageGatewayConnection (final JmsMessageGatewayProperties jmsConfig) throws JMSException {
    return new JmsMessageGatewayConnection(jmsConfig, cachingConnectionFactory(jmsConfig));
}

虽然@DependsOn可以确保JmsMessageGatewayProperties Bean在JmsMessageGatewayConnection之前被创建,但它并不能解决属性绑定本身的问题。如果JmsMessageGatewayProperties的属性在创建时就是null,那么即使它先被创建,其内部属性仍然是null。@DependsOn主要用于解决Bean之间的初始化顺序依赖,而不是属性加载问题。因此,对于本场景,它不是一个直接有效的解决方案。

注意事项与最佳实践

  1. Spring Boot vs. 纯Spring框架:
    • Spring Boot: 提供了@ConfigurationPropertiesScan和@EnableConfigurationProperties等便利机制,简化了属性绑定。通常推荐在@ConfigurationProperties类上不加@PropertySource,而是通过application.properties/application.yml或@ConfigurationPropertiesScan来统一管理。
    • 纯Spring: 需要更明确地使用@PropertySource来指定属性文件,确保属性加载到Environment中。
  2. @TestPropertySource的作用: TestPropertySource主要用于在测试环境中覆盖或添加属性到Spring的Environment。它本身不会自动触发@ConfigurationProperties的绑定,需要配合@PropertySource或@ConfigurationPropertiesScan等机制。
  3. 调试技巧: 如果属性仍然为null,可以在JmsMessageGatewayProperties的构造函数或setter方法上设置断点,检查属性值在何时何地被赋值。同时,可以检查Spring的Environment中是否包含预期的属性(例如,通过@Autowired Environment env并打印env.getProperty("jms.remoteUri"))。
  4. 属性文件命名: 确保@PropertySource或@TestPropertySource中指定的属性文件路径和名称是正确的。classpath:前缀表示从类路径加载。

总结

为Spring @Configuration类及其依赖的@ConfigurationProperties编写单元测试时,确保外部属性能够正确绑定是关键。核心在于让Spring容器知道去哪里找到这些属性,并激活@ConfigurationProperties的绑定机制。

  • 对于纯Spring应用或需要明确指定属性文件的场景,在@ConfigurationProperties类或其配置类上使用@PropertySource 是最常见且有效的解决方案。
  • 对于Spring Boot应用,@ConfigurationPropertiesScan 提供了一种更自动化、更简洁的方式来管理和绑定配置属性。
  • @DependsOn主要解决Bean创建顺序问题,不能直接解决属性绑定问题

通过理解这些机制并选择合适的策略,开发者可以构建出健壮且易于维护的Spring配置单元测试。

相关专题

更多
spring框架介绍
spring框架介绍

本专题整合了spring框架相关内容,想了解更多详细内容,请阅读专题下面的文章。

102

2025.08.06

spring boot框架优点
spring boot框架优点

spring boot框架的优点有简化配置、快速开发、内嵌服务器、微服务支持、自动化测试和生态系统支持。本专题为大家提供spring boot相关的文章、下载、课程内容,供大家免费下载体验。

135

2023.09.05

spring框架有哪些
spring框架有哪些

spring框架有Spring Core、Spring MVC、Spring Data、Spring Security、Spring AOP和Spring Boot。详细介绍:1、Spring Core,通过将对象的创建和依赖关系的管理交给容器来实现,从而降低了组件之间的耦合度;2、Spring MVC,提供基于模型-视图-控制器的架构,用于开发灵活和可扩展的Web应用程序等。

389

2023.10.12

Java Spring Boot开发
Java Spring Boot开发

本专题围绕 Java 主流开发框架 Spring Boot 展开,系统讲解依赖注入、配置管理、数据访问、RESTful API、微服务架构与安全认证等核心知识,并通过电商平台、博客系统与企业管理系统等项目实战,帮助学员掌握使用 Spring Boot 快速开发高效、稳定的企业级应用。

68

2025.08.19

Java Spring Boot 4更新教程_Java Spring Boot 4有哪些新特性
Java Spring Boot 4更新教程_Java Spring Boot 4有哪些新特性

Spring Boot 是一个基于 Spring 框架的 Java 开发框架,它通过 约定优于配置的原则,大幅简化了 Spring 应用的初始搭建、配置和开发过程,让开发者可以快速构建独立的、生产级别的 Spring 应用,无需繁琐的样板配置,通常集成嵌入式服务器(如 Tomcat),提供“开箱即用”的体验,是构建微服务和 Web 应用的流行工具。

31

2025.12.22

Java Spring Boot 微服务实战
Java Spring Boot 微服务实战

本专题深入讲解 Java Spring Boot 在微服务架构中的应用,内容涵盖服务注册与发现、REST API开发、配置中心、负载均衡、熔断与限流、日志与监控。通过实际项目案例(如电商订单系统),帮助开发者掌握 从单体应用迁移到高可用微服务系统的完整流程与实战能力。

114

2025.12.24

c语言中null和NULL的区别
c语言中null和NULL的区别

c语言中null和NULL的区别是:null是C语言中的一个宏定义,通常用来表示一个空指针,可以用于初始化指针变量,或者在条件语句中判断指针是否为空;NULL是C语言中的一个预定义常量,通常用来表示一个空值,用于表示一个空的指针、空的指针数组或者空的结构体指针。

231

2023.09.22

java中null的用法
java中null的用法

在Java中,null表示一个引用类型的变量不指向任何对象。可以将null赋值给任何引用类型的变量,包括类、接口、数组、字符串等。想了解更多null的相关内容,可以阅读本专题下面的文章。

435

2024.03.01

Java 桌面应用开发(JavaFX 实战)
Java 桌面应用开发(JavaFX 实战)

本专题系统讲解 Java 在桌面应用开发领域的实战应用,重点围绕 JavaFX 框架,涵盖界面布局、控件使用、事件处理、FXML、样式美化(CSS)、多线程与UI响应优化,以及桌面应用的打包与发布。通过完整示例项目,帮助学习者掌握 使用 Java 构建现代化、跨平台桌面应用程序的核心能力。

2

2026.01.14

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
Kotlin 教程
Kotlin 教程

共23课时 | 2.5万人学习

C# 教程
C# 教程

共94课时 | 6.7万人学习

Java 教程
Java 教程

共578课时 | 45.8万人学习

关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号