[英]Reading metrics configuration from YAML file within a dropwizard REST application
[英]How to test Externalized Configuration reading from yaml
這是我第一次使用外部配置和 yaml。
我創建了一個 yaml,其中我使用 class 名稱作為 KEY,字段名稱作為 VALUE
YAML:
# test placeholders
project:
test:
service:
computator:
# exclude field from beeing randomly valorized
population:
exclude:
InputClass: 'myDate'
AnotherClass: 'myName'
排除人口屬性:
@Data
@Component
@ConfigurationProperties(prefix = "project")
public class ExcludePopulationProperties {
private Test test;
@Data
public static class Test {
private Service service;
}
@Data
public static class Service {
private Computator computator;
}
@Data
public static class Computator {
private Population population;
}
@Data
public static class Population {
private Map<String, String> exclude;
}
}
使用 JUnit 5 進行測試:
@ContextConfiguration(classes = { ExcludePopulationProperties.class })
@ExtendWith(SpringExtension.class)
class YamlTest {
@Autowired
private ExcludePopulationProperties excludePopulationProperties;
@Test
void testExternalConfiguration() {
Map<String, String> map = excludePopulationProperties.getTest().getService().getComputator().getPopulation().getExclude();
assertNotNull(map);
}
問題是我有一個 NullPointerException 因為測試是 null
所以我不確定這里出了什么問題,我期待 map 被正確填充。
我也嘗試添加
@TestPropertySource(properties = { "spring.config.location=classpath:application-_test.yaml" })
在 YamlTest 上
您的ExcludePopulationProperties
類不應使用@Component
注釋。 相反,您應該在項目中的配置類上添加注釋@EnableConfigurationProperties(ExcludePopulationProperties.class)
(主應用程序類將起作用)。
將您的屬性類更改為如下所示(刪除@Component
):
@Data
@ConfigurationProperties(prefix = "project")
public class ExcludePopulationProperties {
...
}
更改您的應用程序類以啟用配置屬性:
@SpringBootApplication
@EnableConfigurationProperties(ExcludePopulationProperties.class)
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
YamlTest
類看起來像這樣(使用@SpringBootTest
):
@SpringBootTest
@ContextConfiguration(classes = { DemoApplication.class })
@TestPropertySource(properties = { "spring.config.location=classpath:application-test.yaml" })
class YamlTest {
@Autowired
private ExcludePopulationProperties excludePopulationProperties;
@Test
void testExternalConfiguration() {
Map<String, String> map = excludePopulationProperties.getTest().getService().getComputator().getPopulation().getExclude();
assertNotNull(map);
assertEquals(map.get("InputClass"), "myDate");
assertEquals(map.get("AnotherClass"), "myName");
}
}
通過這些小改動,現在我可以測試 YAML 文件中的屬性值。
目前它僅適用於 Spring,不適用於 Mockito。 一旦我找到 mockito 的解決方案,我就會更新答案。
我稍微改進了 yaml,
# test placeholders
project:
test:
service:
computator:
# exclude field from beeing randomly valorized
population:
exclude:
InputClass:
- 'myDate'
AnotherClass:
- 'myName'
所以現在 ExcludePopulationProperties 有一個 Map<String, List> 而不是 Map<String, String>,這樣我就可以從同一個 class 中排除多個字段:
@Data
@Configuration
@ConfigurationProperties(prefix = "project")
@PropertySource(value = "classpath:application-_test.yaml", factory = YamlPropertySourceFactory.class)
public class ExcludePopulationProperties {
private Test test;
@Data
public static class Test {
private Service service;
}
@Data
public static class Service {
private Computator computator;
}
@Data
public static class Computator {
private Population population;
}
@Data
public static class Population {
private Map<String, List<String>> exclude;
}
}
YamlPropertySourceFactory 是 Baeldung 在本指南中實現的 class: @PropertySource with YAML Files in Spring Boot
public class YamlPropertySourceFactory implements PropertySourceFactory {
@Override
public PropertySource<?> createPropertySource(String name, EncodedResource resource) throws IOException {
YamlPropertiesFactoryBean factory = new YamlPropertiesFactoryBean();
factory.setResources(resource.getResource());
Properties properties = factory.getObject();
return new PropertiesPropertySource(resource.getResource().getFilename(), properties);
}
}
使用 SpringExtension 對 JUnit 5 進行測試:
@EnableConfigurationProperties(value = { ExcludePopulationProperties.class })
@TestPropertySource(properties = { "spring.config.location=classpath:application-_test.yaml" })
@ExtendWith(SpringExtension.class)
class YamlTest {
@Autowired
private ExcludePopulationProperties excludePopulationProperties;
@Test
void testExternalConfiguration() {
Map<String, List<String>> map = excludePopulationProperties.getTest().getService().getComputator().getPopulation().getExclude();
assertNotNull(map);
}
}
PS 目前它不能與 MockitoExtension 一起使用,這對於具有模擬依賴項的單元測試 class 來說是一個大問題
它也不適用於其他類型的配置,例如
@ContextConfiguration(classes = FactoriesConfig.class)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.