-
Notifications
You must be signed in to change notification settings - Fork 41.2k
Using @ActiveProfiles with @SpringBootTest now adds to the profiles configured using spring.profiles.active rather than overriding them #19788
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Thanks for the report. I suspect that this is a side-effect of the changes for #19556. |
#19556 is the cause. Prior to that change, You can get 2.2.2's behaviour in 2.2.3 by removing
In some ways, I think 2.2.3's behaviour is better. It makes it possible to add an active profile while also making it possible to override the active profiles as well. In 2.2.2, only overriding entirely was possible. However, the change in behaviour in a maintenance release isn't ideal. Flagging for team attention so that we can consider if we want to restore the old behaviour in 2.2.4 which would require finding a different solution for #19556. |
I should also have mentioned that using |
We have seen breakage in our tests which could be related to this. The
java.lang.IllegalArgumentException: Could not resolve placeholder 'spring.profiles.active' in value "${spring.profiles.active}" |
@dogilvie Thank you. That is the same problem. It may well be addressed by this issue, but I would encourage you not to rely upon it. You should consider the use of the |
I wondered if we could fix this without breaking the fix for #19556 by setting diff --git a/spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/context/SpringBootContextLoader.java b/spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/context/SpringBootContextLoader.java
index 99fb6a5f5b..71350fbe12 100644
--- a/spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/context/SpringBootContextLoader.java
+++ b/spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/context/SpringBootContextLoader.java
@@ -29,6 +29,7 @@ import org.springframework.boot.context.properties.source.ConfigurationPropertyS
import org.springframework.boot.context.properties.source.MapConfigurationPropertySource;
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
import org.springframework.boot.test.mock.web.SpringBootMockServletContext;
+import org.springframework.boot.test.util.TestPropertyValues;
import org.springframework.boot.web.reactive.context.GenericReactiveWebApplicationContext;
import org.springframework.boot.web.servlet.support.ServletContextApplicationContextInitializer;
import org.springframework.context.ApplicationContext;
@@ -97,7 +98,7 @@ public class SpringBootContextLoader extends AbstractContextLoader {
application.getSources().addAll(Arrays.asList(configLocations));
ConfigurableEnvironment environment = getEnvironment();
if (!ObjectUtils.isEmpty(config.getActiveProfiles())) {
- environment.setActiveProfiles(config.getActiveProfiles());
+ setActiveProfiles(environment, config.getActiveProfiles());
}
ResourceLoader resourceLoader = (application.getResourceLoader() != null) ? application.getResourceLoader()
: new DefaultResourceLoader(getClass().getClassLoader());
@@ -125,6 +126,14 @@ public class SpringBootContextLoader extends AbstractContextLoader {
return application.run(getArgs(config));
}
+ private void setActiveProfiles(ConfigurableEnvironment environment, String[] profiles) {
+ List<String> pairs = new ArrayList<>();
+ for (int i = 0; i < profiles.length; i++) {
+ pairs.add("spring.profiles.active[" + i + "]=" + profiles[i]);
+ }
+ TestPropertyValues.of(pairs).applyTo(environment);
+ }
+
/**
* Builds new {@link org.springframework.boot.SpringApplication} instance. You can
* override this method to add custom behavior It works in terms of In short, there are other areas where profiles with commas in their names do not work as might be expected. #19537 was a hypothetical problem so I think we should revert the changes made in #19556 and re-open #19537 to see if it's something that we want to try and support in the future. |
@wilkinsona can you explain further why you take this view? It seemed to me that the property and the annotation are both independent mechanisms for writing the "spring active profiles". And the property had the additional function of being able to read back the "spring active profiles" value. I would suggest a new |
Largely because it isn't documented anywhere. The purpose of Application code that depends upon the |
Let me ask - what is the best way to run tests with the |
When I said "depends upon" above, I really meant consumes. I wouldn't expect test or main code to consume |
Hello, I have active profile issues with Spring Boot 2.2.3.RELEASE.
Step to reproduce :
spring.profiles.active=prod
toapplication.properties
@ExtendWith(SpringExtension.class)
and@ActiveProfiles("test")
toPetclinicIntegrationTests
PetclinicIntegrationTests
You can see:
The following profiles are active: test
diplayed in logsNow change
spring-boot-starter-parent
to2.2.3.RELEASE
and re-run the test.You can see:
The following profiles are active: test,prod
diplayed in logsThe text was updated successfully, but these errors were encountered: