【发布时间】:2021-03-17 00:13:56
【问题描述】:
我正在尝试测试从我的 application.yml 文件到 POJO 类的简单属性绑定。当我使用点表示法设置属性时,我可以让我的单元测试打印出属性值,但是当我将相同的属性更改为 .yml 表示时,我的单元测试会打印出一个空值。我在这里遗漏了什么吗?
# this works
blah.resourcePrefix=blah
# this does not work
blah:
resourcePrefix: blah
@ConfigurationProperties(prefix = "blah")
public class JustTesting {
private String resourcePrefix;
public String getResourcePrefix() {
return resourcePrefix;
}
public void setResourcePrefix(String resourcePrefix) {
this.resourcePrefix = resourcePrefix;
}
}
@ExtendWith(SpringExtension.class)
@EnableConfigurationProperties(value = JustTesting.class)
@TestPropertySource("classpath:application.yml")
public class PropertiesTest {
@Autowired
JustTesting justTesting;
@Test
public void testProperties(){
System.out.println(justTesting.getResourcePrefix());
}
}
【问题讨论】:
标签: java spring properties