
在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加载
Post AI
博客文章AI生成器
50
查看详情
如果不想修改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之间的初始化顺序依赖,而不是属性加载问题。因此,对于本场景,它不是一个直接有效的解决方案。
注意事项与最佳实践-
Spring Boot vs. 纯Spring框架:
- Spring Boot: 提供了@ConfigurationPropertiesScan和@EnableConfigurationProperties等便利机制,简化了属性绑定。通常推荐在@ConfigurationProperties类上不加@PropertySource,而是通过application.properties/application.yml或@ConfigurationPropertiesScan来统一管理。
- 纯Spring: 需要更明确地使用@PropertySource来指定属性文件,确保属性加载到Environment中。
- @TestPropertySource的作用: TestPropertySource主要用于在测试环境中覆盖或添加属性到Spring的Environment。它本身不会自动触发@ConfigurationProperties的绑定,需要配合@PropertySource或@ConfigurationPropertiesScan等机制。
- 调试技巧: 如果属性仍然为null,可以在JmsMessageGatewayProperties的构造函数或setter方法上设置断点,检查属性值在何时何地被赋值。同时,可以检查Spring的Environment中是否包含预期的属性(例如,通过@Autowired Environment env并打印env.getProperty("jms.remoteUri"))。
- 属性文件命名: 确保@PropertySource或@TestPropertySource中指定的属性文件路径和名称是正确的。classpath:前缀表示从类路径加载。
为Spring @Configuration类及其依赖的@ConfigurationProperties编写单元测试时,确保外部属性能够正确绑定是关键。核心在于让Spring容器知道去哪里找到这些属性,并激活@ConfigurationProperties的绑定机制。
- 对于纯Spring应用或需要明确指定属性文件的场景,在@ConfigurationProperties类或其配置类上使用@PropertySource 是最常见且有效的解决方案。
- 对于Spring Boot应用,@ConfigurationPropertiesScan 提供了一种更自动化、更简洁的方式来管理和绑定配置属性。
- @DependsOn主要解决Bean创建顺序问题,不能直接解决属性绑定问题。
通过理解这些机制并选择合适的策略,开发者可以构建出健壮且易于维护的Spring配置单元测试。
以上就是Spring配置类与外部属性单元测试实践指南的详细内容,更多请关注知识资源分享宝库其它相关文章!
相关标签: word java app 常见问题 spring框架 spring容器 red gate spring spring boot NULL 构造函数 对象 自动化 大家都在看: 如何用Java操作Word?Apache POI教程 使用Java下载文件时,为什么Word和PPT文件会变成乱码的TXT文件? Freemarker生成的Word文档中如何调整w:pict标签内图片大小? Freemarker生成Word文档:如何控制图片大小? PHPWord插件读取Word文档字符串失败:如何解决TypeError错误?






发表评论:
◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。