Your ExcludePopulationProperties
class should not be annotated with @Component
. Instead, you should have the annotation @EnableConfigurationProperties(ExcludePopulationProperties.class)
on a configuration class in your project (the main application class will work).
Change your properties class to look like this (removing @Component
):
@Data
@ConfigurationProperties(prefix = "project")
public class ExcludePopulationProperties {
...
}
Change your application class to enable the configuration properties:
@SpringBootApplication
@EnableConfigurationProperties(ExcludePopulationProperties.class)
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
And the YamlTest
class to look like this (using @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");
}
}