question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Profile properties not loading during integration tests

See original GitHub issue

I’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:closed
  • Created 8 years ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
dlvenablecommented, Jun 22, 2015

I changed my integration test to:

@ContextConfiguration(loader = SpringApplicationContextLoader.class, classes = MyApplication.class)
@ActiveProfiles("development")
@WebIntegrationTest
@Stepwise
class MySpec extends Specification

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.

0reactions
dlvenablecommented, Jun 24, 2015

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found