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.
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.
We can use @Value annotation if you have few configs or other wise we can use a config properties class. For e.g
If you need to have production
application.yml
completely replaced then put its test version to the same path but in test environment (usually it issrc/test/resources/
)But if you need to override or add some properties then you have few options.
Option 1: put test
application.yml
insrc/test/resources/config/
directory as @TheKojuEffect suggests in his answer.Option 2: use profile-specific properties: create say
application-test.yml
in yoursrc/test/resources/
folder and:add
@ActiveProfiles
annotation to your test classes:or alternatively set
spring.profiles.active
property value in@SpringBootTest
annotation:This works not only with
@SpringBootTest
but with@JsonTest
,@JdbcTests
,@DataJpaTest
and other slice test annotations as well.And you can set as many profiles as you want (
spring.profiles.active=dev,hsqldb
) - see farther details in documentation on Profiles.Starting with Spring 4.1, We can directly set the property in application.yml using the @TestPropertySource annotation.
Just convert your yaml parameters into complete property structure. For example: If content of application.yml is like below
Then value to go inside the @TestPropertySource will be,
You can set your test properties in
src/test/resources/config/application.yml
file. Spring Boot test cases will take properties fromapplication.yml
file in test directory.Spring-boot framework allows us to provide YAML files as a replacement for the .properties file and it is convenient.The keys in property files can be provided in YAML format in application.yml file in the resource folder and spring-boot will automatically take it up.Keep in mind that the yaml format has to keep the spaces correct for the value to be read correctly.
You can use the
@Value("${property}")
to inject the values from the YAML files. Also Spring.active.profiles can be given, to differentiate between different YAML for different environments for convenient deployment.For testing purposes, the test yaml file can be name like application-test.yml and placed in the resource folder of the test directory.
If you are specifying the
applciation-test.yml
and provide the spring test profile in the yml, then you can use the@ActiveProfiles('test')
annotation to direct spring to take the configurations from the application-test.yml that you have specified.If you are using JUnit 5 then no need for other annotations as @SpringBootTest already include the springrunner annotation. Keeping a separate main ApplicationTest.class enables us to provide separate configuration classes for tests and we can prevent the default configuration beans from loading by excluding them from component scan in the test main class. You can also provide the profile to be loaded there.
Here is the link for Spring documentation regarding use of YAML instead of
.properties
file(s): https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.htmlOne 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: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.