With following configuration, my test can read the properties from the yaml file correctly.
@SpringBootApplication
@PropertySource("classpath:application.yml")
@ComponentScan({ "com.my.service" })
public class MyApplication {
}
Then I renamed the yaml file to my-application.yml, and changed the PropertySource to
@PropertySource("classpath:my-application.yml")
Tests are failed due to the null property value. The configuration class is as following:
@Configuration
@ConfigurationProperties(prefix="my")
@Data
public class MyConfig {
private String attr1;
}
The test class is:
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = MyApplication.class)
public class MyConfigTest {
@Autowired
private MyConfig myConfig;
@Test
public void getMyConfigTest() {
Assert.assertNotNull(myConfig.getAttr1());
}
Why spring boot can find the renamed yaml file, but it couldn't load the value correctly?