java – 在测试中使用Spring @ConfigurationProperties读取一个Map
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了java – 在测试中使用Spring @ConfigurationProperties读取一个Map,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含4049字,纯文字阅读大概需要6分钟。
内容图文
![java – 在测试中使用Spring @ConfigurationProperties读取一个Map](/upload/InfoBanner/zyjiaocheng/774/ec2439e7f495476ca2225b4bbbed7050.jpg)
根据Spring Boot integration tests doesn’t read properties files的建议,我创建了以下代码,目的是从我的JUnit测试中的属性中读取地图.
(我使用yml格式,并使用@ConfigurationProperties而不是@Value)
@RunWith(SpringJUnit4ClassRunner.class)
@TestPropertySource(locations="classpath:application-test.yml")
@ContextConfiguration(classes = {PropertiesTest.ConfigurationClass.class, PropertiesTest.ClassToTest.class})
public class PropertiesTest {
@Configuration
@EnableConfigurationProperties
static class ConfigurationClass {
}
@ConfigurationProperties
static class ClassToTest {
private String test;
private Map<String, Object> myMap = new HashMap<>();
public String getTest() {
return test;
}
public void setTest(String test) {
this.test = test;
}
public Map<String, Object> getMyMap() {
return myMap;
}
}
@Autowired
private ClassToTest config;
@Test
public void testStringConfig() {
Assert.assertEquals(config.test, "works!");
}
@Test
public void testMapConfig() {
Assert.assertEquals(config.myMap.size(), 1);
}
}
我的测试配置(在application-test.yml中):
test: works!
myMap:
aKey: aVal
aKey2: aVal2
奇怪的是,字符串“有效!”已成功从配置文件中读取,但未填充地图.
我错过了什么?
注意:添加地图设置器会导致以下异常:
Caused by: org.springframework.validation.BindException: org.springframework.boot.bind.RelaxedDataBinder$RelaxedBeanPropertyBindingResult: 1 errors
Field error in object 'target' on field 'myMap': rejected value []; codes [typeMismatch.target.myMap,typeMismatch.myMap,typeMismatch.java.util.Map,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [target.myMap,myMap]; arguments []; default message [myMap]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'java.util.Map' for property 'myMap'; nested exception is java.lang.IllegalStateException: Cannot convert value of type 'java.lang.String' to required type 'java.util.Map' for property 'myMap': no matching editors or conversion strategy found]
at org.springframework.boot.bind.PropertiesConfigurationFactory.checkForBindingErrors(PropertiesConfigurationFactory.java:359)
at org.springframework.boot.bind.PropertiesConfigurationFactory.doBindPropertiesToTarget(PropertiesConfigurationFactory.java:276)
at org.springframework.boot.bind.PropertiesConfigurationFactory.bindPropertiesToTarget(PropertiesConfigurationFactory.java:240)
at org.springframework.boot.context.properties.ConfigurationPropertiesBindingPostProcessor.postProcessBeforeInitialization(ConfigurationPropertiesBindingPostProcessor.java:330)
... 42 more
解决方法:
经过一段调试器的美好时光,
我相信这是TestPropertySourceUtils.addPropertiesFilesToEnvironment()中的错误/缺失功能:
try {
for (String location : locations) {
String resolvedLocation = environment.resolveRequiredPlaceholders(location);
Resource resource = resourceLoader.getResource(resolvedLocation);
environment.getPropertySources().addFirst(new ResourcePropertySource(resource));
}
}
ResourcePropertySource只能处理.properties文件,而不能处理.yml.
在常规应用程序中,YamlPropertySourceLoader已注册并可以处理.yml.
作为一个说明:
TestPropertySourceUtils.addPropertiesFilesToEnvironment()由以下方法调用:
org.springframework.test.context.support.DelegatingSmartContextLoader.prepareContext()
(继承自AbstractContextLoader)
如果在@ContextConfiguration中未指定加载器,DelegatingSmartContextLoader是您收到的默认上下文加载器.
(实际上@ContextConfiguration指定了一个接口,但AbstractTestContextBootstrapper.resolveContextLoader()将其更改为具体类)
要解决此问题,我将配置更改为application-test.properties
并在我的测试中使用该文件.
test=works!
myMap.aKey: aVal
另一条评论:不需要地图上的setter:
To bind to properties like that using the Spring DataBinder utilities
(which is what @ConfigurationProperties does) you need to have a
property in the target bean of type java.util.List (or Set) and you
either need to provide a setter, or initialize it with a mutable
value, e.g. this will bind to the properties above
内容总结
以上是互联网集市为您收集整理的java – 在测试中使用Spring @ConfigurationProperties读取一个Map全部内容,希望文章能够帮你解决java – 在测试中使用Spring @ConfigurationProperties读取一个Map所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。