Skip to content
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

@ActiveProfiles is not honored on @SpringBootApplication even in test context #32771

Closed
xenoterracide opened this issue May 6, 2024 · 5 comments
Assignees
Labels
in: test Issues in the test module status: invalid An issue that we don't feel is valid

Comments

@xenoterracide
Copy link

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"'.

bug-property-source.tar.gz

original bug response: spring-projects/spring-boot#40620 (comment)

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label May 6, 2024
@sansay61
Copy link

sansay61 commented May 6, 2024

@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

@sbrannen sbrannen closed this as not planned Won't fix, can't repro, duplicate, stale May 6, 2024
@sbrannen sbrannen self-assigned this May 6, 2024
@sbrannen sbrannen added in: test Issues in the test module status: invalid An issue that we don't feel is valid and removed status: waiting-for-triage An issue we've not yet triaged or decided on labels May 6, 2024
@xenoterracide
Copy link
Author

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).

@sbrannen sbrannen changed the title ActiveProfiles not used on SpringBootApplication even in test context @ActiveProfiles is not honored on @SpringBootApplication even in test context May 6, 2024
@sbrannen
Copy link
Member

sbrannen commented May 7, 2024

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.

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.

@snicoll
Copy link
Member

snicoll commented May 7, 2024

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.

@xenoterracide
Copy link
Author

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 @Configurations

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: test Issues in the test module status: invalid An issue that we don't feel is valid
Projects
None yet
Development

No branches or pull requests

5 participants