1

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.

5
  • Is the file in /src/test/resources or src/main/resources? If it is in src/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. Commented Nov 22, 2016 at 15:24
  • It is in src/main/resources but I think it is best to create one in src/test/resources for test purpose, how could I do it ? Commented Nov 22, 2016 at 17:16
  • Copy and paste the file. The downside is you will now have to duplicate everything. Commented Nov 22, 2016 at 18:24
  • And if I want to keep using the file in src/main/resources (it is actually the case, but not working) how to proceed ? Commented Nov 23, 2016 at 7:06
  • Assuming you are using gradle, you want to add the /src/main/resources to your "test" source set. Add something like the following to your build.gradle: sourceSets { test { resources [${project.projectDir}/src/${sourceSet.name}/resources], [${project.projectDir}/src/main/resources] } } Commented Nov 24, 2016 at 18:43

2 Answers 2

3

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.

@RunWith(SpringJUnit4ClassRunner.class) @SpringApplicationConfiguration(classes = YourApplication.class) public class InternationalizationTests() { @Autowired ConnectionProperties connectionProperties; @Test 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: http://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-testing.html

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

1 Comment

I updated my post with code, I can't load the file properly.
0

Unit test can be done easily with Jmockit

 import org.junit.jupiter.api.Test; import org.springframework.boot.SpringApplication; import org.springframework.context.ConfigurableApplicationContext; import mockit.Mock; import mockit.MockUp; import mockit.Mocked; import mockit.Verifications; class RuleApiApplicationTest { @Mocked private ConfigurableApplicationContext mockedContext; @Test void testApplicationRun() { new MockUp<SpringApplication>() { @Mock public ConfigurableApplicationContext run(Class<?> primarySource, String... args) { return mockedContext; } }; RuleApiApplication.main(new String[]{}); new Verifications() {{ SpringApplication.run(RuleApiApplication.class, new String[]{}); }}; } } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.