It would seem that ActiveProfiles
isn't detected when on a SpringBootApplication
maybe this is intentional, but I guess I was expecting it to be respected on a configuration.
package org.example.bugpropertysource;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.test.context.ActiveProfiles;
@ActiveProfiles("test")
@SpringBootApplication
public class BugPropertySourceApplication {
}
# application-test.properties
spring.main.banner-mode=off
15:35:14.637 [Test worker] INFO org.springframework.test.context.support.AnnotationConfigContextLoaderUtils -- Could not detect default configuration classes for test class [org.example.bugpropertysource.BugPropertySourceApplicationTests]: BugPropertySourceApplicationTests does not declare any static, non-private, non-final, nested classes annotated with @Configuration.
15:35:14.709 [Test worker] INFO org.springframework.boot.test.context.SpringBootTestContextBootstrapper -- Found @SpringBootConfiguration org.example.bugpropertysource.BugPropertySourceApplication for test class org.example.bugpropertysource.BugPropertySourceApplicationTests
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v3.2.5)
2024-05-03T15:35:15.013-04:00 INFO 2069502 --- [bug-property-source] [ Test worker] o.e.b.BugPropertySourceApplicationTests : Starting BugPropertySourceApplicationTests using Java 17.0.8 with PID 2069502 (started by xeno in /home/xeno/IdeaProjects/bug-property-source)
2024-05-03T15:35:15.015-04:00 INFO 2069502 --- [bug-property-source] [ Test worker] o.e.b.BugPropertySourceApplicationTests : No active profile set, falling back to 1 default profile: "default"
2024-05-03T15:35:15.860-04:00 INFO 2069502 --- [bug-property-source] [ Test worker] o.e.b.BugPropertySourceApplicationTests : Started BugPropertySourceApplicationTests in 1.042 seconds (process running for 1.819)
OpenJDK 64-Bit Server VM warning: Sharing is only supported for boot loader classes because bootstrap classpath has been appended
> Task :test
Deprecated Gradle features were used in this build, making it incompatible with Gradle 9.0.
You can use '--warning-mode all' to show the individual deprecation warnings and determine if they come from your own scripts or plugins.
For more on this, please refer to https://docs.gradle.org/8.7/userguide/command_line_interface.html#sec:command_line_warnings in the Gradle documentation.
BUILD SUCCESSFUL in 2s
4 actionable tasks: 1 executed, 3 up-to-date
3:35:16 PM: Execution finished ':test --tests "org.example.bugpropertysource.BugPropertySourceApplicationTests"'.
original bug response: https://github.com/spring-projects/spring-boot/issues/40620#issuecomment-2094066565
Comment From: sansay61
@ActiveProfiles
is a class-level annotation that is used to declare which bean definition profiles should be active when loading an ApplicationContext for an integration test. (c)
https://docs.spring.io/spring-framework/reference/testing/annotations/integration-spring/annotation-activeprofiles.html
PS. change your test to
@ActiveProfiles("test")
@SpringBootTest
class BugPropertySourceApplicationTests {
@Autowired
Environment env;
@Test
void contextLoads() {
System.out.println("Active profile:" + Arrays.toString(env.getActiveProfiles()));
}
}
and you'll see its working and no banner appeared in the stdout
Comment From: xenoterracide
I'm aware of that, which is why I said "even when in a test context". Since Spring Boot needs some kind of context class I always use the same TestApplication
class/module for almost all of my test suites (honestly that's a different annoyance).
Comment From: sbrannen
It would seem that
ActiveProfiles
isn't detected when on aSpringBootApplication
maybe this is intentional, but I guess I was expecting it to be respected on a configuration.
That is indeed intentional,
@ActiveProfiles
and similar annotations in the TestContext framework are only honored when present on a test class.
In other words, they are not honored on a @Configuration
class or other such "production" classes.
In light of that, I've closed this issue as invalid.
However, I'll improve the wording in the Javadoc for such annotations in #32772.
Comment From: snicoll
To provide a bit more context to the "why", it's a test-only annotation (as you are aware) that participates in the context key that the TCF uses to cache contexts, and this needs to be known upfront. Irrespective of that, you can't decide to activate a profile in flight (i.e. while processing configuration classes).
That hopefully explains why you can't expect to use it elsewhere than the test class. It's a bit like enabling an extra profile on SpringApplication
before it runs.
Comment From: xenoterracide
yeah, I'm speculating I thought it would work because the @SpringBootApplication
probably has to be processed "first", and I'd guess only once... like before, or just after application.properties
unlike general @Configuration
s