0

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?

1
  • Can you post content of property file? Commented Dec 3, 2015 at 11:12

1 Answer 1

2

YAML files can’t be loaded via the @PropertySource annotation

It appears to work with @PropertySource("classpath:application.yml") because that's the default location and spring boot looks there regardless.

You may be able to use @ConfigurationProperties(location="claspath:my-application.yml") instead but it doesn't really achieve the same purpose (and I've never tried it myself).

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for your answer. BTW, I have tried @ConfigurationProperties(location="claspath:my-application.yml") , it doesn't work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.