我有一个Spring Boot应用程序,它有一些配置属性.我正在尝试为某些组件编写测试,并希望从test.propertIEs文件加载配置属性.我无法让它发挥作用.
这是我的代码:
test.propertIEs文件(在src / test / resources下):
vehicleSequence.propagationTreeMaxSize=10000
配置属性类:
package com.acme.foo.vehiclesequence.config;import javax.valIDation.constraints.NotNull;import org.springframework.boot.context.propertIEs.ConfigurationPropertIEs;import org.springframework.stereotype.Component;@Component@ConfigurationPropertIEs(prefix = VehicleSequenceConfigurationPropertIEs.PREFIX)public class VehicleSequenceConfigurationPropertIEs { static final String PREFIX = "vehicleSequence"; @NotNull private Integer propagationTreeMaxSize; public Integer getPropagationTreeMaxSize() { return propagationTreeMaxSize; } public voID setPropagationTreeMaxSize(Integer propagationTreeMaxSize) { this.propagationTreeMaxSize = propagationTreeMaxSize; }}
我的测试:
@RunWith(springrunner.class)@ContextConfiguration(classes = VehicleSequenceConfigurationPropertIEs.class)@TestPropertySource("/test.propertIEs")public class VehicleSequenceConfigurationPropertIEsTest { @autowired private VehicleSequenceConfigurationPropertIEs vehicleSequenceConfigurationPropertIEs; @Test public voID checkPropagationTreeMaxSize() { assertthat(vehicleSequenceConfigurationPropertIEs.getPropagationTreeMaxSize()).isEqualTo(10000); }}
测试失败并显示“Expecting actual not to null”表示未设置配置属性类中的属性propagationTreeMaxSize.最佳答案发布问题两分钟后,我找到了答案.
我必须使用@EnableConfigurationPropertIEs(VehicleSequenceConfigurationPropertIEs.class)启用配置属性:
@RunWith(springrunner.class)@TestPropertySource("/test.propertIEs")@EnableConfigurationPropertIEs(VehicleSequenceConfigurationPropertIEs.class)public class VehicleSequenceConfigurationPropertIEsTest { @autowired private VehicleSequenceConfigurationPropertIEs vehicleSequenceConfigurationPropertIEs; @Test public voID checkPropagationTreeMaxSize() { assertthat(vehicleSequenceConfigurationPropertIEs.getPropagationTreeMaxSize()).isEqualTo(10000); }}
总结 以上是内存溢出为你收集整理的java – Spring Test中未加载配置属性全部内容,希望文章能够帮你解决java – Spring Test中未加载配置属性所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)