Profile properties not loading during integration tests
See original GitHub issueI’m trying to create some integration tests which run my Spring Boot application with a specified Spring profile. From my experimentation, it seems that I can set the profile, but Spring Boot does not load the properties file for that profile
@SpringApplicationConfiguration(classes = MyApplication.class, initializers = MyInitializer.class)
@WebIntegrationTest
@Stepwise
class MySpec extends Specification
{
}
class MyInitializer implements ApplicationContextInitializer<ConfigurableApplicationContext>
{
@Override
void initialize(ConfigurableApplicationContext applicationContext)
{
applicationContext.getEnvironment().setActiveProfiles('test')
}
}
Using the debugger I can see that the Spring Environment
object does have my active profile. But, the properties for my profile are not loaded.
To clarify, by properties file, I mean files in resources like: application.properties application-test.properties
I’ve tried this in Spring Boot 1.2.3 and 1.2.4. I am building with Gradle and testing with Spock.
Issue Analytics
- State:
- Created 8 years ago
- Comments:6 (2 by maintainers)
Top Results From Across the Web
Spring Boot profile properties doesn't work with tests
Now when I build with command mvn clean package -Dspring. profiles. active=dev , properties src/main/resources/application-dev.
Read more >Setting Default Spring Profile for Tests with Override Option
The obvious solution was to use Spring profiles and a custom properties file since Spring reads, among others, application-{profile}.properties file where ...
Read more >Override Properties in Spring's Tests - Baeldung
In this tutorial, we'll look at various ways to override the properties in Spring's tests. Spring actually provides a number of solutions ...
Read more >Testing - Spring
This chapter covers Spring's support for integration testing and best practices for unit testing. The Spring team advocates test-driven ...
Read more >Testing with Spring Boot and @SpringBootTest - Reflectoring
Before we start into integration tests with Spring Boot, ... or application-<profile>.yml and load the properties from that file by ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
I changed my integration test to:
I ran the application through the debugger and inspecting an
@Autowired Environment
I see the active profile development (it appears that there was an existing test so I changed my profile name). However, the profile properties are not being loaded for classes that need them.I created a sample project, but was unable to reproduce the problem. Previously I had been running my tests from IntelliJ. I am now doing it from the Gradle build and it works. Oddly enough, in the sample project the tests do run from IntelliJ. So the issue here probably lies in IntelliJ.