-
Notifications
You must be signed in to change notification settings - Fork 38.4k
ActiveProfiles not included when using a custom annotation. [SPR-13748] #18321
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
Adam Berlin commented If it helps, I worked with Rossen when creating this ticket. He should have additional context. |
Sam Brannen commented For starters, your example won't work since it does not define the retention policy. Have you tried the following? @Retention(RetentionPolicy.RUNTIME)
@ActiveProfiles("foo")
public @interface MyActiveProfile {
} |
Adam Berlin commented Adding the retention to this example worked, but I have a more complicated example that does not work: package holmes.rtvs.repositories;
import org.springframework.test.context.ActiveProfiles;
@ActiveProfiles({"foo"})
public class ReproduceBaseTest {
} @RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(loader = SpringApplicationContextLoader.class)
@ReproduceTest.MyActiveProfile
public class ReproduceTest extends ReproduceBaseTest {
@Autowired
private String value;
@Test
public void itDoesNotPickUpTestPropertiesUsingMetaAnnotation() {
assertEquals("bar", this.value);
}
@Retention(RetentionPolicy.RUNTIME)
@ActiveProfiles({"bar"})
public static @interface MyActiveProfile {
}
@Configuration
@Profile("foo")
public static class FooConfig {
@Bean
public String value() {
return "foo";
}
}
@Configuration
@Profile("bar")
public static class BarConfig {
@Bean
public String value() {
return "bar";
}
}
} |
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: @ContextConfiguration Annotating your class like this causes it to fail: @ContextConfiguration(classes = {
ReproduceTest.BarConfig.class,
ReproduceTest.FooConfig.class }) Annotating your class like this causes it to pass: @ContextConfiguration(classes = {
ReproduceTest.FooConfig.class,
ReproduceTest.BarConfig.class }) I am therefore closing this issue as "Works as Designed". Regards, Sam |
Rossen Stoyanchev commented
They are two beans of the same type and name but they're included in separate config files each marked with |
Sam Brannen commented
In the
The end result is that Does that make things clearer? |
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 |
Sam Brannen commented FYI: See also: |
Rossen Stoyanchev commented Okay here is an example: @ActiveProfiles(value = "bar")
class ReproduceBaseTest {
}
@Retention(RetentionPolicy.RUNTIME)
@ActiveProfiles(value = "foo", inheritProfiles = false)
@interface MyActiveProfiles {
}
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration
@MyActiveProfiles
public class ReproduceTest extends ReproduceBaseTest {
@Autowired
private ConfigurableApplicationContext context;
@Test
public void itDoesNotPickUpTestPropertiesUsingMetaAnnotation() {
String[] profiles = this.context.getEnvironment().getActiveProfiles();
System.out.println(Arrays.toString(profiles));
}
@Configuration
public static class FooConfig {
}
} In master it prints "[foo]" correctly. In 4.1.x it prints "[bar]". |
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 |
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 |
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. |
Adam Berlin opened SPR-13748 and commented
The following doesn't work:
Affects: 4.1.8
Issue Links:
The text was updated successfully, but these errors were encountered: