
在spring boot项目中,我们经常使用@configuration注解定义配置类来创建bean,并结合@configurationproperties将外部配置文件中的属性绑定到pojo对象上,实现配置的外部化。然而,在对这类配置进行单元测试时,一个常见的挑战是尽管使用了@testpropertysource指定了属性文件,但绑定到@configurationproperties的pojo对象中的属性值却仍然为null,导致依赖这些属性的bean无法正确初始化,抛出如invalid uri: cannot be null or empty之类的错误。
例如,考虑以下配置结构:
JmsMessageGatewayConnectionConfig.java (配置类)
@Configuration
public class JmsMessageGatewayConnectionConfig {
@Bean
public JmsMessageGatewayConnection jmsMessageGatewayConnection (final JmsMessageGatewayProperties jmsConfig) throws JMSException {
return new JmsMessageGatewayConnection(jmsConfig, cachingConnectionFactory(jmsConfig));
}
// ... 其他私有方法和Bean定义 ...
@Bean
@ConfigurationProperties(prefix = "jms")
public JmsMessageGatewayProperties messageGatewayProperties() {
return new JmsMessageGatewayProperties();
}
}JmsMessageGatewayProperties.java (属性POJO)
public class JmsMessageGatewayProperties {
private String remoteUri;
private String username;
private String password;
// ... getters and setters ...
}JmsMessageGatewayConnectionConfigTest.java (测试类)
@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.remoteUri=vm://localhost:61616等属性时,测试仍然失败,提示remoteUri为null。这表明@TestPropertySource虽然加载了属性文件,但JmsMessageGatewayProperties对象并未成功绑定这些属性。
问题的根源在于Spring加载属性文件和绑定@ConfigurationProperties的时机和机制。@TestPropertySource负责将属性加载到Spring的Environment中,但@ConfigurationProperties的绑定还需要额外的步骤来确保这些属性被解析并注入到对应的POJO中。在某些情况下,尤其是在不完全依赖Spring Boot自动配置的单元测试环境中,可能需要显式地指导Spring完成这个绑定过程。
最直接的解决方案是在@ConfigurationProperties所在的类(或其包含的@Configuration类)上使用@PropertySource注解,明确指出属性文件的位置。这样可以确保在@ConfigurationProperties尝试绑定属性之前,这些属性已经被加载并可供使用。
方法一:在JmsMessageGatewayProperties类上添加@PropertySource
@PropertySource("classpath:camel.properties") // 添加此行
public class JmsMessageGatewayProperties {
private String remoteUri;
private String username;
private String password;
// ... getters and setters ...
}这种方式的优点是属性POJO自身携带了其属性来源信息,但在测试场景下可能不够灵活,因为它绑定了特定的属性文件路径。
方法二:在JmsMessageGatewayConnectionConfig配置类上添加@PropertySource
@Configuration
@PropertySource("classpath:camel.properties") // 添加此行
public class JmsMessageGatewayConnectionConfig {
// ... 保持不变 ...
}这种方式更常见,因为它将属性源的声明集中在配置类中,与@ConfigurationProperties的Bean定义更紧密。在测试中,@TestPropertySource会覆盖或补充@PropertySource中定义的属性,因此这种结合使用是有效的。
通过上述任一方法,Spring在初始化JmsMessageGatewayProperties Bean时,将能够找到并绑定camel.properties中定义的属性。
对于Spring Boot 2.2及更高版本,Spring引入了@ConfigurationPropertiesScan注解,极大地简化了@ConfigurationProperties类的注册和管理。此注解会自动扫描指定包下的@ConfigurationProperties类,并将其注册为Spring Bean。
使用方法:
在主应用类上使用(适用于集成测试或完整应用启动)
@SpringBootApplication
@ConfigurationPropertiesScan("com.example.config") // 扫描JmsMessageGatewayProperties所在的包
public class MyApplication {
public static void main(String[] args) {
SpringApplication.run(MyApplication.class, args);
}
}在测试配置类上使用(适用于单元测试或组件测试) 如果你的测试不是基于@SpringBootTest而是@ContextConfiguration,你可以在一个专门的测试配置类上使用它,或者直接在@ContextConfiguration指向的配置类中包含它。
例如,可以在JmsMessageGatewayConnectionConfig上添加:
@Configuration
@ConfigurationPropertiesScan // 默认扫描当前包及其子包
public class JmsMessageGatewayConnectionConfig {
// ...
}或者,如果你的测试使用@SpringBootTest,则通常无需额外配置,@SpringBootApplication本身就包含了组件扫描功能,可以发现@ConfigurationProperties。
@ConfigurationPropertiesScan的优势在于,它提供了一种更自动化的方式来发现和注册@ConfigurationProperties Bean,减少了手动@Bean方法或@PropertySource的依赖,使得配置更简洁。
无论采用哪种解决方案,你的单元测试类结构都将保持简洁,因为属性加载的逻辑已转移到配置或属性类本身。
@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);
// 进一步验证jmsMessageGatewayConnection内部属性是否正确,例如
// Assert.assertEquals("vm://localhost:61616", jmsMessageGatewayConnection.getJmsConfig().getRemoteUri());
}
}@TestPropertySource在此处的作用是确保camel.properties中的属性被加载到测试环境的Environment中,供@PropertySource或@ConfigurationPropertiesScan发现并绑定。
对Spring @Configuration和@ConfigurationProperties进行单元测试时,确保外部属性正确加载和绑定是关键。通过在@ConfigurationProperties类或其所在的@Configuration类上使用@PropertySource,可以显式地指定属性源。对于Spring Boot 2.2及更高版本,@ConfigurationPropertiesScan提供了一种更自动化和现代化的方式来管理@ConfigurationProperties。结合@TestPropertySource,我们可以构建健壮且可信赖的Spring配置单元测试。理解这些机制有助于避免常见的null指针异常,并确保应用程序配置的正确性。
以上就是Spring配置类与属性绑定单元测试指南的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号