Skip to content

Commit 18e9699

Browse files
committed
Demo @ContextConfiguration as composed annotation with ACI
This commit demonstrates how to register one or more @configuration classes via an ApplicationContextInitializer in a composed annotation so that certain @configuration classes are always registered whenever the composed annotation is used, even if the composed annotation is used to declare additional @configuration classes.
1 parent 4cf0b59 commit 18e9699

File tree

3 files changed

+160
-0
lines changed

3 files changed

+160
-0
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* Copyright 2002-2016 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.test.context.junit4.aci.annotation;
18+
19+
import org.springframework.context.annotation.Bean;
20+
import org.springframework.context.annotation.Configuration;
21+
22+
/**
23+
* @author Sam Brannen
24+
* @since 4.3
25+
*/
26+
@Configuration
27+
class BarConfig {
28+
29+
@Bean
30+
String bar() {
31+
return "bar";
32+
}
33+
34+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* Copyright 2002-2016 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.test.context.junit4.aci.annotation;
18+
19+
import org.springframework.context.annotation.Bean;
20+
import org.springframework.context.annotation.Configuration;
21+
22+
/**
23+
* @author Sam Brannen
24+
* @since 4.3
25+
*/
26+
@Configuration
27+
class FooConfig {
28+
29+
@Bean
30+
String foo() {
31+
return "foo";
32+
}
33+
34+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
/*
2+
* Copyright 2002-2016 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.test.context.junit4.aci.annotation;
18+
19+
import java.lang.annotation.ElementType;
20+
import java.lang.annotation.Retention;
21+
import java.lang.annotation.RetentionPolicy;
22+
import java.lang.annotation.Target;
23+
import java.util.List;
24+
25+
import org.junit.Test;
26+
import org.junit.runner.RunWith;
27+
import org.springframework.beans.factory.annotation.Autowired;
28+
import org.springframework.context.ApplicationContextInitializer;
29+
import org.springframework.context.annotation.AnnotatedBeanDefinitionReader;
30+
import org.springframework.context.support.GenericApplicationContext;
31+
import org.springframework.core.annotation.AliasFor;
32+
import org.springframework.test.context.ContextConfiguration;
33+
import org.springframework.test.context.junit4.SpringRunner;
34+
import org.springframework.test.context.junit4.aci.annotation.InitializerConfiguredViaMetaAnnotationTests.ComposedContextConfiguration;
35+
import org.springframework.test.context.support.AnnotationConfigContextLoader;
36+
37+
import static org.junit.Assert.assertEquals;
38+
39+
/**
40+
* Integration test that demonstrates how to register one or more {@code @Configuration}
41+
* classes via an {@link ApplicationContextInitializer} in a composed annotation so
42+
* that certain {@code @Configuration} classes are always registered whenever the composed
43+
* annotation is used, even if the composed annotation is used to declare additional
44+
* {@code @Configuration} classes.
45+
*
46+
* <p>This class has been implemented in response to the following Stack Overflow question:
47+
* <a href="http://stackoverflow.com/questions/35733344/can-contextconfiguration-in-a-custom-annotation-be-merged">
48+
* Can {@code @ContextConfiguration} in a custom annotation be merged?</a>
49+
*
50+
* @author Sam Brannen
51+
* @since 4.3
52+
*/
53+
@RunWith(SpringRunner.class)
54+
@ComposedContextConfiguration(BarConfig.class)
55+
public class InitializerConfiguredViaMetaAnnotationTests {
56+
57+
@Autowired
58+
String foo;
59+
60+
@Autowired
61+
String bar;
62+
63+
@Autowired
64+
List<String> strings;
65+
66+
67+
@Test
68+
public void beansFromInitializerAndComposedAnnotation() {
69+
assertEquals(2, strings.size());
70+
assertEquals("foo", foo);
71+
assertEquals("bar", bar);
72+
}
73+
74+
75+
static class FooConfigInitializer implements ApplicationContextInitializer<GenericApplicationContext> {
76+
77+
@Override
78+
public void initialize(GenericApplicationContext applicationContext) {
79+
new AnnotatedBeanDefinitionReader(applicationContext).register(FooConfig.class);
80+
}
81+
}
82+
83+
@ContextConfiguration(loader = AnnotationConfigContextLoader.class, initializers = FooConfigInitializer.class)
84+
@Retention(RetentionPolicy.RUNTIME)
85+
@Target(ElementType.TYPE)
86+
@interface ComposedContextConfiguration {
87+
88+
@AliasFor(annotation = ContextConfiguration.class, attribute = "classes")
89+
Class<?>[] value() default {};
90+
}
91+
92+
}

0 commit comments

Comments
 (0)