I created a spring-boot 1.4.0 application and I would like to internationlize it using yaml file.
I created a class for loading the configuration from the yaml file like it is explained in the documentation here http://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html#boot-features-external-config-typesafe-configuration-properties.
I would like to create a test to check that my class has correctly loaded the properties from the yaml file.
If we keep the exemple from the documentation how to create a unit test that will load a yaml file (with a different name that application.yml) and check that the method getUsername() will return the value from the yaml file ?
Here is the code I have but still can't load the username :
@Component @ConfigurationProperties(locations = "classpath:mylocalizedprops.yml", prefix="connection") public class ConnectionProperties { private String username; // ... getters and setters } and the test class
@RunWith(SpringJUnit4ClassRunner.class) @SpringBootTest(classes = Application.class) public class InternationalizationTest { @Autowired private ConnectionProperties connectionProperties; public void propsShouldBeNotNull() { assertNotNull(connectionProperties); } public void userNameShouldBeCorrect() { assertEquals(connectionProperties.getUsername(), expectedUserName); } } I have failed the userNameShouldBeCorrect test. The file mylocalizedprops.yml is located in the src/main/resources folder of a Maven structured application.
/src/test/resourcesorsrc/main/resources? If it is insrc/main/resources, you will need to either put it in the test resources, or load the main source set as part of your test source sets./src/main/resourcesto your "test" source set. Add something like the following to yourbuild.gradle:sourceSets { test { resources [${project.projectDir}/src/${sourceSet.name}/resources], [${project.projectDir}/src/main/resources] } }