Tests with ActiveProfiles annotation cannot resolve placeholder 'spring.profiles.active'

See original GitHub issue

With 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:closed
  • Created 3 years ago
  • Reactions:3
  • Comments:8 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
aratnam86commented, Aug 4, 2020

Thank you @mbhave . It works, if we move the configuration file out of the application to an external location and then using spring.config.import to include it.

Also, thank you for your tips on using #--- and spring.config.activate.on-profile. Very nice features.

0reactions
mbhavecommented, Jul 30, 2020

@aratnam86 With 2.4.x, we have added the concept of importing additional configuration files using the spring.config.import property. For your application, I can imagine an application.properties file that looks something like this:

#---
spring.config.activate.on-profile=dev
spring.config.import=pearl-services-dev.properties,ruby-services-dev.properties,diamond-service-dev.properties
#---
spring.config.activate.on-profile=prod
spring.config.import=pearl-services-prod.properties,ruby-services-prod.properties,diamond-service-prod.properties

For tests, you can add another application.properties file that imports the test specific configuration. The #--- syntax is also new in the 2.4.x line which is support for multi-document .properties files.

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.active property, you should set it explicitly, using the properties attribute of @SpringBootTest, rather than relying on the implementation details of @ActiveProfiles.

Read more comments on GitHub >

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

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