Tests with ActiveProfiles annotation cannot resolve placeholder 'spring.profiles.active'
See original GitHub issueWith version Spring Boot version 2.3.0.RELEASE, tests annotated with @ActiveProfiles("test") fail to resolve variable spring.profiles.active.
The issue is not present on version 2.2.7.RELEASE.
It can be reproduced with a simple Spring Boot app created from Spring Initializr with options:
- Maven Project
- Java
- Spring Boot 2.3.0
- Packaging Jar
- Java 8
- No dependencies
The code to add are a Config class, @ActiveProfiles on the test class, and a application-test.properties file (empty contents).
Config class
package com.example.demo;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
@Configuration
@PropertySource(
ignoreResourceNotFound = false,
value = "classpath:application-${spring.profiles.active}.properties")
public class Config {}
Test file with @ActiveProfiles
@SpringBootTest
@ActiveProfiles("test")
class DemoApplicationTests {
@Test
void contextLoads() {}
}
Issue Analytics
- State:
- Created 3 years ago
- Reactions:3
- Comments:8 (2 by maintainers)
Top Results From Across the Web
Could not resolve placeholder 'spring.profiles.active' in value ...
I am trying read ldap properties from a ldap-TEST.properties file and trying to bind it to ...
Read more >spring-projects/spring-boot - Gitter
However if I try to run an application which uses this library with Spring Boot, I get the "Could not resolve placeholder 'spring.profiles.active'”...
Read more >24. Externalized Configuration - Spring
Spring Boot allows you to externalize your configuration so you can work with the same application code in different environments. You can use...
Read more >Setting Default Spring Profile for Tests with Override Option
Talk with our tech expert & find initial solutions the challenge, that is currently ... There are few ways to setup active profiles...
Read more >[Spring Error] Could not resolve placeholder - Taogen's Blog
Solutions. Check whether the value of the property spring.profiles.active is correct in application.yml . Check whether the property ...
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
Thank you @mbhave . It works, if we move the configuration file out of the application to an external location and then using
spring.config.importto include it.Also, thank you for your tips on using
#---andspring.config.activate.on-profile. Very nice features.@aratnam86 With 2.4.x, we have added the concept of importing additional configuration files using the
spring.config.importproperty. For your application, I can imagine anapplication.propertiesfile that looks something like this:For tests, you can add another
application.propertiesfile that imports the test specific configuration. The#---syntax is also new in the 2.4.x line which is support for multi-document.propertiesfiles.We’ve added support for importing additional files in the 2.4.x line so you can give it a try using the
2.4.0-SNAPSHOTs. For more details, please see the reference guide.Could you let us know if this meets your needs and if not then please provide a minimal sample that we can run to understand the use case further.
That being said, if your code relies on reading the
spring.profiles.activeproperty, you should set it explicitly, using thepropertiesattribute of@SpringBootTest, rather than relying on the implementation details of@ActiveProfiles.