Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Load different application.yml in SpringBoot Test

I'm using a spring boot app which runs my src/main/resources/config/application.yml.

When I run my test case by :

@RunWith(SpringJUnit4ClassRunner.class) @SpringApplicationConfiguration(classes = Application.class) @WebAppConfiguration @IntegrationTest public class MyIntTest{ } 

The test codes still run my application.yml file to load properties. I wonder if it is possible to run another *.yml file when running the test case.

like image 578
Exia Avatar asked Aug 02 '16 04:08

Exia


People also ask

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

Loading different yml file If you want to totally load different yml file for test you can use locations attribute on @TestPropertySource . @TestPropertySource(locations="classpath:test. yml") @RunWith(SpringJUnit4ClassRunner.

What are the different ways to load YAML file in spring boot?

Spring Framework provides two convenient classes that can be used to load YAML documents. The YamlPropertiesFactoryBean will load YAML as Properties and the YamlMapFactoryBean will load YAML as a Map.

How do you use ConfigurationProperties in a test?

Basically, you: use a specific configuration to @EnableConfigurationProperties and @EnableAutoConfiguration , listing all the @ConfigurationProperties files you want to load. in the test class, you load this configuration file of tests, with an initializer class defined by Spring to load application. yml file.


2 Answers

One option is to work with profiles. Create a file called application-test.yml, move all properties you need for those tests to that file and then add the @ActiveProfiles annotation to your test class:

@RunWith(SpringJUnit4ClassRunner.class) @SpringApplicationConfiguration(classes = Application.class) @WebAppConfiguration @IntegrationTest @ActiveProfiles("test") // Like this public class MyIntTest{ } 

Be aware, it will additionally load the application-test.yml, so all properties that are in application.yml are still going to be applied as well. If you don't want that, either use a profile for those as well, or override them in your application-test.yml.

like image 123
g00glen00b Avatar answered Oct 05 '22 20:10

g00glen00b


You can set your test properties in src/test/resources/config/application.yml file. Spring Boot test cases will take properties from application.yml file in test directory.

The config folder is predefined in Spring Boot.

As per documentation:

If you do not like application.properties as the configuration file name, you can switch to another file name by specifying a spring.config.name environment property. You can also refer to an explicit location by using the spring.config.location environment property (which is a comma-separated list of directory locations or file paths). The following example shows how to specify a different file name:

java -jar myproject.jar --spring.config.location=classpath:/default.properties,classpath:/override.properties 

The same works for application.yml

Documentation:

https://docs.spring.io/spring-boot/docs/current/reference/html/spring-boot-features.html#boot-features-external-config-application-property-files

https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html#boot-features-external-config-application-property-files

like image 45
TheKojuEffect Avatar answered Oct 05 '22 22:10

TheKojuEffect