jerome jerome - 2 years ago 387
Java Question

SpringBoot unit test configuration

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

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

will return the value from the yaml file ?

Here is the code I have but still can't load the username :

@ConfigurationProperties(locations = "classpath:mylocalizedprops.yml", prefix="connection")
public class ConnectionProperties {

private String username;

// ... getters and setters


and the test class

@SpringBootTest(classes = Application.class)
public class InternationalizationTest {
private ConnectionProperties connectionProperties;

public void propsShouldBeNotNull() {

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.

Answer Source

I would consider this an integration test, not a unit-test because you are testing the interaction between various components. Regardless, here is how I would do it.

@SpringApplicationConfiguration(classes = YourApplication.class)
public class InternationalizationTests() {

  ConnectionProperties connectionProperties;

  public void testCorrectTranslationLoaded() {
    Assert.assertEquals("english-username", connectionProperties.getUsername());


You can also create a test configuration if you would like to, which you can specify which translation to load. You would then need different classes to test different configurations. See the docs:

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download