【发布时间】:2018-07-16 13:32:51
【问题描述】:
根据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
【问题讨论】:
-
您的地图没有设置器。这就是它不起作用的原因。 Spring 调用 setter 来填充值
-
类似的bean在没有setter的完整应用程序中成功创建(可以添加map值而不需要设置整个map)。在测试中添加 setter 以异常结束 - 添加了有问题的堆栈跟踪
-
我猜
String, Object不太合适。您是否尝试过<String, String>只是因为它不知道如何将 aVal2 反序列化为Object? -
没有。要使 ConfigurationProperties 工作,您需要一个 setter。没有它是行不通的。它可以很容易地验证,只需删除设置器进行测试,看看是否有效。我确信它不会。现在在添加 setter 时遇到异常,我认为这是 Databinder 中的错误。我将值类型更改为字符串,仍然不起作用。删除地图的初始化,仍然没有工作。但是如果我把同样的东西放在 application.properties 文件中它就可以了。它很奇怪。你应该在 github 上提出问题
-
我在文档中检查并在我的计算机上进行了验证:您是对的。如果没有 setter,它将无法以这种方式工作。但是这个仍然有效:stackoverflow.com/a/28764090/4068240 老实说,我真的不明白为什么。