小编Mis*_*sus的帖子

Spring Boot测试@ConfigurationProperties不绑定字段

各位晚上好。我有一个专为与属性绑定而设计的类。在标记为 的类中@Configuration,我将这个 bean 对象设置为:

\n\n
// Into TargetConfiguration class\n\n@Bean("someBeanName")\n@ConfigurationProperties("some.path")\npublic beanProperties() {\n    return new BeanProperties();\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

我想测试此配置的所有功能,为此我创建一个测试类,例如:

\n\n
@WebFluxTest\n@TestPropertySource("classpath:test.properties")\n@ContextConfiguration(classes = {TargetConfiguration.class})\nclass Test {...}\n
Run Code Online (Sandbox Code Playgroud)\n\n

问题是它将BeanProperties包含空字段,但如果我创建一些其他字段并通过inOtherBeanProperties注册它,那么它通常会被填充,但对于我可以\xe2\x80\x99t 这样做,因为这个bean取决于测试中的属性。特性。另外,如果我通过配置创建这个 bean ,那么它将被填充到...@EnableConfigurationPropertiesTargetConfigurationBeanProperties@TestConfiguration

\n\n

任何帮助表示赞赏。

\n\n

UPD-1:第一个假设 - 我注册了一些使用名为 的注释扩展BeanDefinitionRegistryPostProcessor@DependsOnRegistratorClass。我不知道为什么,但是如果我添加@DependsOnRegistratorClass,我会BeanProperties立即构造而不绑定(并传递给想要接收此消息的其他方法BeanProperties)。如果我删除这个注释,RegistratorClass初始化第一个注释,我的BeanProperties将正常构造并绑定到属性。

\n

java spring spring-boot spring-boot-test

5
推荐指数
1
解决办法
1256
查看次数

标签 统计

java ×1

spring ×1

spring-boot ×1

spring-boot-test ×1