Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Boot properties in 'application.yml' not loading from JUnit Test

What am I doing wrong? I'm using this little standalone App which runs and finds my src/main/resources/config/application.yml. The same configuration doesn't work from JUnit, see below:

@Configuration @ComponentScan @EnableConfigurationProperties  public class TestApplication {      public static void main(String[] args) {          SpringApplication.run(TestApplication.class);     } }   @Component @ConfigurationProperties  public class Bean{     ... } 

The following doesn't work, the same properties in application.yml are not loaded and Bean has only null values:

@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(classes = TestApplication.class)  public class SomeTestClass {     ... } 
like image 858
aliopi Avatar asked Oct 19 '15 12:10

aliopi


People also ask

How do you load a yml file in junit test cases spring boot?

We can use @SpringBootTest annotation which loads the yml file from src\main\java\com... hence when we execute the unit test, all of the properties are already there in the config properties class. @Data @Component @RefreshScope @ConfigurationProperties(prefix = "address.

How do I read test properties in spring boot?

You can use @TestPropertySource annotation in your test class. Just annotate @TestPropertySource("classpath:config/mailing. properties") on your test class. You should be able to read out the property for example with the @Value annotation.

How can we access the YAML properties in spring boot?

To access the YAML properties, we'll create an object of the YAMLConfig class, and access the properties using that object. In the properties file, we'll set the spring. profiles. active environment variable to prod.

How do I access application yml properties?

To read from the application. yml or property file : The easiest way to read a value from the property file or yml is to use the spring @value annotation.


2 Answers

Try this:

@RunWith(SpringJUnit4ClassRunner.class) @SpringApplicationConfiguration(classes = TestApplication.class,      initializers = ConfigFileApplicationContextInitializer.class) public class SomeTestClass {     ... } 

EDIT:

For Spring Boot version 1.5+, SpringApplicationConfiguration was removed in favour of SpringBootTest or direct use of SpringBootContextLoader.

You can still use initializers parameter with ContextConfiguration annotation.

like image 160
luboskrnac Avatar answered Sep 17 '22 15:09

luboskrnac


The trick to load any custom yml file in SpringBoot 2.0 w/o using @SpringBootTest

  • create some yml file in test\resources
  • Use ConfigFileApplicationContextInitializer and spring.config.location property

Example Code:

@RunWith(SpringRunner.class) @ContextConfiguration(     classes = { MyConfiguration.class, AnotherDependancy.class },     initializers = {ConfigFileApplicationContextInitializer.class} ) @TestPropertySource(properties = { "spring.config.location=classpath:myApp-test.yml" }) public class ConfigProviderTest {     @Autowired     private MyConfiguration myConfiguration; //this will be filled with myApp-test.yml      @Value("${my.config-yml-string}")    private String someSrting; //will get value from the yml file.  } 

For JUnit 5 use the @ExtendWith(SpringExtension.class) annotation instead of @RunWith(SpringRunner.class)

like image 27
Yarix Avatar answered Sep 18 '22 15:09

Yarix