diff --git a/spring-test/src/test/java/org/springframework/test/context/junit4/aci/annotation/BarConfig.java b/spring-test/src/test/java/org/springframework/test/context/junit4/aci/annotation/BarConfig.java new file mode 100644 index 0000000000..1ee6b6fd9c --- /dev/null +++ b/spring-test/src/test/java/org/springframework/test/context/junit4/aci/annotation/BarConfig.java @@ -0,0 +1,34 @@ +/* + * Copyright 2002-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.test.context.junit4.aci.annotation; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +/** + * @author Sam Brannen + * @since 4.3 + */ +@Configuration +class BarConfig { + + @Bean + String bar() { + return "bar"; + } + +} \ No newline at end of file diff --git a/spring-test/src/test/java/org/springframework/test/context/junit4/aci/annotation/FooConfig.java b/spring-test/src/test/java/org/springframework/test/context/junit4/aci/annotation/FooConfig.java new file mode 100644 index 0000000000..a080fd84a4 --- /dev/null +++ b/spring-test/src/test/java/org/springframework/test/context/junit4/aci/annotation/FooConfig.java @@ -0,0 +1,34 @@ +/* + * Copyright 2002-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.test.context.junit4.aci.annotation; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +/** + * @author Sam Brannen + * @since 4.3 + */ +@Configuration +class FooConfig { + + @Bean + String foo() { + return "foo"; + } + +} \ No newline at end of file diff --git a/spring-test/src/test/java/org/springframework/test/context/junit4/aci/annotation/InitializerConfiguredViaMetaAnnotationTests.java b/spring-test/src/test/java/org/springframework/test/context/junit4/aci/annotation/InitializerConfiguredViaMetaAnnotationTests.java new file mode 100644 index 0000000000..971e3f4b12 --- /dev/null +++ b/spring-test/src/test/java/org/springframework/test/context/junit4/aci/annotation/InitializerConfiguredViaMetaAnnotationTests.java @@ -0,0 +1,92 @@ +/* + * Copyright 2002-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.test.context.junit4.aci.annotation; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; +import java.util.List; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.ApplicationContextInitializer; +import org.springframework.context.annotation.AnnotatedBeanDefinitionReader; +import org.springframework.context.support.GenericApplicationContext; +import org.springframework.core.annotation.AliasFor; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringRunner; +import org.springframework.test.context.junit4.aci.annotation.InitializerConfiguredViaMetaAnnotationTests.ComposedContextConfiguration; +import org.springframework.test.context.support.AnnotationConfigContextLoader; + +import static org.junit.Assert.assertEquals; + +/** + * Integration test that demonstrates how to register one or more {@code @Configuration} + * classes via an {@link ApplicationContextInitializer} in a composed annotation so + * that certain {@code @Configuration} classes are always registered whenever the composed + * annotation is used, even if the composed annotation is used to declare additional + * {@code @Configuration} classes. + * + *

This class has been implemented in response to the following Stack Overflow question: + * + * Can {@code @ContextConfiguration} in a custom annotation be merged? + * + * @author Sam Brannen + * @since 4.3 + */ +@RunWith(SpringRunner.class) +@ComposedContextConfiguration(BarConfig.class) +public class InitializerConfiguredViaMetaAnnotationTests { + + @Autowired + String foo; + + @Autowired + String bar; + + @Autowired + List strings; + + + @Test + public void beansFromInitializerAndComposedAnnotation() { + assertEquals(2, strings.size()); + assertEquals("foo", foo); + assertEquals("bar", bar); + } + + + static class FooConfigInitializer implements ApplicationContextInitializer { + + @Override + public void initialize(GenericApplicationContext applicationContext) { + new AnnotatedBeanDefinitionReader(applicationContext).register(FooConfig.class); + } + } + + @ContextConfiguration(loader = AnnotationConfigContextLoader.class, initializers = FooConfigInitializer.class) + @Retention(RetentionPolicy.RUNTIME) + @Target(ElementType.TYPE) + @interface ComposedContextConfiguration { + + @AliasFor(annotation = ContextConfiguration.class, attribute = "classes") + Class[] value() default {}; + } + +}