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.
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.
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.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With