You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
ActiveProfiles not included when using a custom annotation. [SPR-13748] #18321
FYI: this has nothing to do with the testing framework or meta-annotation support. Rather, this is simply a configuration error.
You are defining two beans of the same type with the same name, and you cannot have two of the same thing. So naturally the last one wins. It just so happens that "foo" always wins in this particular case (at least on my JVM).
Annotating your class like this causes it to fail:
@ContextConfiguration
Annotating your class like this causes it to fail:
You are defining two beans of the same type with the same name, and you cannot have two of the same thing.
They are two beans of the same type and name but they're included in separate config files each marked with @Profile. In combination with @ActiveProfiles shouldn't only one of them be included. In fact when not using the custom annotation, i.e. using @ActiveProfile, it works.
@ActiveProfiles works analogous to setting the spring.profiles.active system property.
In the ReproduceTest example in the comments above, both profiles (i.e., foo and bar) are active, and that's to be expected, since:
ReproduceBaseTest declares @ActiveProfiles("foo")
ReproduceTest declares @ReproduceTest.MyActiveProfile which declares @ActiveProfiles("bar")
As explained in the Javadoc, active profiles are inherited by default within test class hierarchies. If you don't want profiles to be inherited, you can disable this feature.
The end result is that foo and bar are both active profiles; any components that are declared for either of those profiles are thus active in the application context; and for duplicate beans of the same name and type, the last one wins. I demonstrated this above by changing the order in which the @Configuration classes are registered/detected. Again, these are the standard semantics for Spring profiles; the testing framework does not change the semantics for how profiles work.
In fact when not using the custom annotation, i.e. using @ActiveProfile, it works.
I was not able to reproduce this behavior. So if you can provide an example that demonstrates that the testing framework handles profiles differently than production deployments, that would be a bug, and I'd be more than happy to investigate further.
As a general note, there are quite a few composed annotation scenarios which do not work in the 4.1.x line. As a consequence, at this late point, I'm reluctant to try to address any such issues in 4.1.9. Are we ok with marking this issue as resolved in 4.2?
Ok rstoya05-aop, I think we need to find a better example that drives out this problem. I am still unable to create the meta-annotation that I wanted while using Spring 4.2.
Activity
spring-projects-issues commentedon Dec 1, 2015
Adam Berlin commented
If it helps, I worked with Rossen when creating this ticket. He should have additional context.
spring-projects-issues commentedon Dec 2, 2015
Sam Brannen commented
For starters, your example won't work since it does not define the retention policy.
Have you tried the following?
spring-projects-issues commentedon Dec 2, 2015
Adam Berlin commented
Adding the retention to this example worked, but I have a more complicated example that does not work:
spring-projects-issues commentedon Dec 2, 2015
Sam Brannen commented
FYI: this has nothing to do with the testing framework or meta-annotation support. Rather, this is simply a configuration error.
You are defining two beans of the same type with the same name, and you cannot have two of the same thing. So naturally the last one wins. It just so happens that "foo" always wins in this particular case (at least on my JVM).
Annotating your class like this causes it to fail:
Annotating your class like this causes it to fail:
Annotating your class like this causes it to pass:
I am therefore closing this issue as "Works as Designed".
Regards,
Sam
spring-projects-issues commentedon Dec 3, 2015
Rossen Stoyanchev commented
They are two beans of the same type and name but they're included in separate config files each marked with
@Profile. In combination with@ActiveProfilesshouldn't only one of them be included. In fact when not using the custom annotation, i.e. using@ActiveProfile, it works.spring-projects-issues commentedon Dec 3, 2015
Sam Brannen commented
@ActiveProfilesworks analogous to setting thespring.profiles.activesystem property.In the
ReproduceTestexample in the comments above, both profiles (i.e., foo and bar) are active, and that's to be expected, since:ReproduceBaseTestdeclares@ActiveProfiles("foo")ReproduceTestdeclares@ReproduceTest.MyActiveProfilewhich declares@ActiveProfiles("bar")The end result is that
fooandbarare both active profiles; any components that are declared for either of those profiles are thus active in the application context; and for duplicate beans of the same name and type, the last one wins. I demonstrated this above by changing the order in which the@Configurationclasses are registered/detected. Again, these are the standard semantics for Spring profiles; the testing framework does not change the semantics for how profiles work.Does that make things clearer?
spring-projects-issues commentedon Dec 3, 2015
Sam Brannen commented
I was not able to reproduce this behavior. So if you can provide an example that demonstrates that the testing framework handles profiles differently than production deployments, that would be a bug, and I'd be more than happy to investigate further.
Thanks,
Sam
spring-projects-issues commentedon Dec 3, 2015
Sam Brannen commented
FYI:
@ActiveProfileshas been supported as a meta-annotation since Spring Framework 4.0.See also:
spring-projects-issues commentedon Dec 3, 2015
Rossen Stoyanchev commented
Okay here is an example:
In master it prints "[foo]" correctly. In 4.1.x it prints "[bar]".
spring-projects-issues commentedon Dec 3, 2015
Juergen Hoeller commented
As a general note, there are quite a few composed annotation scenarios which do not work in the 4.1.x line. As a consequence, at this late point, I'm reluctant to try to address any such issues in 4.1.9. Are we ok with marking this issue as resolved in 4.2?
Juergen
spring-projects-issues commentedon Dec 3, 2015
Sam Brannen commented
Rossen Stoyanchev, thanks for providing the failing example.
Now it's clear!
This is a duplicate of #17346, which was fixed in 4.2
I am therefore resolving this ticket as a Duplicate.
Regards,
Sam
spring-projects-issues commentedon Dec 3, 2015
Adam Berlin commented
Ok rstoya05-aop, I think we need to find a better example that drives out this problem. I am still unable to create the meta-annotation that I wanted while using Spring 4.2.