
在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能够正确地从测试属性源中加载值,我们有以下几种主要策略:
最直接的解决方案是在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加载
如果不想修改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,并且版本较新(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,它允许你定义一个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 @Configuration类及其依赖的@ConfigurationProperties编写单元测试时,确保外部属性能够正确绑定是关键。核心在于让Spring容器知道去哪里找到这些属性,并激活@ConfigurationProperties的绑定机制。
通过理解这些机制并选择合适的策略,开发者可以构建出健壮且易于维护的Spring配置单元测试。
以上就是Spring配置类与外部属性单元测试实践指南的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号