Skip to content

Commit 4ffeddb

Browse files
tafjwrsbrannen
authored andcommitted
Add tests for ControllerAdviceBean#resolveBean() and revise existing tests
Closes gh-33401
1 parent 59c779d commit 4ffeddb

File tree

1 file changed

+48
-30
lines changed

1 file changed

+48
-30
lines changed

spring-web/src/test/java/org/springframework/web/method/ControllerAdviceBeanTests.java

Lines changed: 48 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -88,48 +88,51 @@ void shouldFailWhenControllerAdviceNull() {
8888
}
8989

9090
@Test
91-
void equalsHashCodeAndToStringForBeanName() {
92-
String beanName = SimpleControllerAdvice.class.getSimpleName();
93-
ControllerAdviceBean bean1 = createControllerAdviceBean(SimpleControllerAdvice.class);
94-
ControllerAdviceBean bean2 = createControllerAdviceBean(SimpleControllerAdvice.class);
91+
void equalsHashCodeAndToString() {
92+
String beanName = getSingletonBeanName(SimpleControllerAdvice.class);
93+
ControllerAdviceBean bean1 = createSingletonControllerAdviceBean(SimpleControllerAdvice.class);
94+
ControllerAdviceBean bean2 = createSingletonControllerAdviceBean(SimpleControllerAdvice.class);
9595
assertEqualsHashCodeAndToString(bean1, bean2, beanName);
9696
}
9797

9898
@Test
99-
void orderedWithLowestPrecedenceByDefaultForBeanName() {
99+
void orderedWithLowestPrecedenceByDefault() {
100100
assertOrder(SimpleControllerAdvice.class, Ordered.LOWEST_PRECEDENCE);
101101
}
102102

103103
@Test
104-
void orderedWithLowestPrecedenceByDefaultForBeanInstance() {
105-
assertOrder(SimpleControllerAdvice.class, Ordered.LOWEST_PRECEDENCE);
106-
}
107-
108-
@Test
109-
void orderedViaOrderedInterfaceForBeanName() {
104+
void orderedViaOrderedInterface() {
110105
assertOrder(OrderedControllerAdvice.class, 42);
111106
}
112107

113108
@Test
114-
void orderedViaOrderedInterfaceForBeanInstance() {
115-
assertOrder(OrderedControllerAdvice.class, 42);
109+
void orderedViaAnnotation() {
110+
assertOrder(OrderAnnotationControllerAdvice.class, 100);
111+
assertOrder(PriorityAnnotationControllerAdvice.class, 200);
116112
}
117113

118114
@Test
119-
void orderedViaAnnotationForBeanName() {
120-
assertOrder(OrderAnnotationControllerAdvice.class, 100);
121-
assertOrder(PriorityAnnotationControllerAdvice.class, 200);
115+
void resolveBeanForSingletonBean() {
116+
ControllerAdviceBean cab = createSingletonControllerAdviceBean(SimpleControllerAdvice.class);
117+
Object bean = this.applicationContext.getBean(getSingletonBeanName(SimpleControllerAdvice.class));
118+
assertThat(cab).extracting("resolvedBean").isNull();
119+
Object resolvedBean = cab.resolveBean();
120+
assertThat(cab).extracting("resolvedBean").isEqualTo(bean);
121+
assertThat(resolvedBean).isEqualTo(bean);
122122
}
123123

124124
@Test
125-
void orderedViaAnnotationForBeanInstance() {
126-
assertOrder(OrderAnnotationControllerAdvice.class, 100);
127-
assertOrder(PriorityAnnotationControllerAdvice.class, 200);
125+
void resolveBeanForNonSingletonBean() {
126+
ControllerAdviceBean cab = createPrototypeControllerAdviceBean(SimpleControllerAdvice.class);
127+
assertThat(cab).extracting("resolvedBean").isNull();
128+
Object resolvedBean = cab.resolveBean();
129+
assertThat(cab).extracting("resolvedBean").isNull();
130+
assertThat(resolvedBean).isInstanceOf(SimpleControllerAdvice.class);
128131
}
129132

130133
@Test
131134
void shouldMatchAll() {
132-
ControllerAdviceBean bean = createControllerAdviceBean(SimpleControllerAdvice.class);
135+
ControllerAdviceBean bean = createSingletonControllerAdviceBean(SimpleControllerAdvice.class);
133136
assertApplicable("should match all", bean, AnnotatedController.class);
134137
assertApplicable("should match all", bean, ImplementationController.class);
135138
assertApplicable("should match all", bean, InheritanceController.class);
@@ -138,7 +141,7 @@ void shouldMatchAll() {
138141

139142
@Test
140143
void basePackageSupport() {
141-
ControllerAdviceBean bean = createControllerAdviceBean(BasePackageSupport.class);
144+
ControllerAdviceBean bean = createSingletonControllerAdviceBean(BasePackageSupport.class);
142145
assertApplicable("base package support", bean, AnnotatedController.class);
143146
assertApplicable("base package support", bean, ImplementationController.class);
144147
assertApplicable("base package support", bean, InheritanceController.class);
@@ -147,7 +150,7 @@ void basePackageSupport() {
147150

148151
@Test
149152
void basePackageValueSupport() {
150-
ControllerAdviceBean bean = createControllerAdviceBean(BasePackageValueSupport.class);
153+
ControllerAdviceBean bean = createSingletonControllerAdviceBean(BasePackageValueSupport.class);
151154
assertApplicable("base package support", bean, AnnotatedController.class);
152155
assertApplicable("base package support", bean, ImplementationController.class);
153156
assertApplicable("base package support", bean, InheritanceController.class);
@@ -156,14 +159,14 @@ void basePackageValueSupport() {
156159

157160
@Test
158161
void annotationSupport() {
159-
ControllerAdviceBean bean = createControllerAdviceBean(AnnotationSupport.class);
162+
ControllerAdviceBean bean = createSingletonControllerAdviceBean(AnnotationSupport.class);
160163
assertApplicable("annotation support", bean, AnnotatedController.class);
161164
assertNotApplicable("this bean is not annotated", bean, InheritanceController.class);
162165
}
163166

164167
@Test
165168
void markerClassSupport() {
166-
ControllerAdviceBean bean = createControllerAdviceBean(MarkerClassSupport.class);
169+
ControllerAdviceBean bean = createSingletonControllerAdviceBean(MarkerClassSupport.class);
167170
assertApplicable("base package class support", bean, AnnotatedController.class);
168171
assertApplicable("base package class support", bean, ImplementationController.class);
169172
assertApplicable("base package class support", bean, InheritanceController.class);
@@ -172,7 +175,7 @@ void markerClassSupport() {
172175

173176
@Test
174177
void shouldNotMatch() {
175-
ControllerAdviceBean bean = createControllerAdviceBean(ShouldNotMatch.class);
178+
ControllerAdviceBean bean = createSingletonControllerAdviceBean(ShouldNotMatch.class);
176179
assertNotApplicable("should not match", bean, AnnotatedController.class);
177180
assertNotApplicable("should not match", bean, ImplementationController.class);
178181
assertNotApplicable("should not match", bean, InheritanceController.class);
@@ -181,7 +184,7 @@ void shouldNotMatch() {
181184

182185
@Test
183186
void assignableTypesSupport() {
184-
ControllerAdviceBean bean = createControllerAdviceBean(AssignableTypesSupport.class);
187+
ControllerAdviceBean bean = createSingletonControllerAdviceBean(AssignableTypesSupport.class);
185188
assertApplicable("controller implements assignable", bean, ImplementationController.class);
186189
assertApplicable("controller inherits assignable", bean, InheritanceController.class);
187190
assertNotApplicable("not assignable", bean, AnnotatedController.class);
@@ -190,7 +193,7 @@ void assignableTypesSupport() {
190193

191194
@Test
192195
void multipleMatch() {
193-
ControllerAdviceBean bean = createControllerAdviceBean(MultipleSelectorsSupport.class);
196+
ControllerAdviceBean bean = createSingletonControllerAdviceBean(MultipleSelectorsSupport.class);
194197
assertApplicable("controller implements assignable", bean, ImplementationController.class);
195198
assertApplicable("controller is annotated", bean, AnnotatedController.class);
196199
assertNotApplicable("should not match", bean, InheritanceController.class);
@@ -216,13 +219,28 @@ public void findAnnotatedBeansSortsBeans() {
216219
assertThat(adviceBeans).extracting(ControllerAdviceBean::getBeanType).containsExactly(expectedTypes);
217220
}
218221

219-
private ControllerAdviceBean createControllerAdviceBean(Class<?> beanType) {
220-
String beanName = beanType.getSimpleName();
222+
private ControllerAdviceBean createSingletonControllerAdviceBean(Class<?> beanType) {
223+
String beanName = getSingletonBeanName(beanType);
221224
this.applicationContext.registerSingleton(beanName, beanType);
222225
ControllerAdvice controllerAdvice = AnnotatedElementUtils.findMergedAnnotation(beanType, ControllerAdvice.class);
223226
return new ControllerAdviceBean(beanName, this.applicationContext, controllerAdvice);
224227
}
225228

229+
private static String getSingletonBeanName(Class<?> beanType) {
230+
return beanType.getSimpleName() + "Singleton";
231+
}
232+
233+
private ControllerAdviceBean createPrototypeControllerAdviceBean(Class<?> beanType) {
234+
String beanName = getPrototypeBeanName(beanType);
235+
this.applicationContext.registerPrototype(beanName, beanType);
236+
ControllerAdvice controllerAdvice = AnnotatedElementUtils.findMergedAnnotation(beanType, ControllerAdvice.class);
237+
return new ControllerAdviceBean(beanName, this.applicationContext, controllerAdvice);
238+
}
239+
240+
private static String getPrototypeBeanName(Class<?> beanType) {
241+
return beanType.getSimpleName() + "Prototype";
242+
}
243+
226244
private void assertEqualsHashCodeAndToString(ControllerAdviceBean bean1, ControllerAdviceBean bean2, String toString) {
227245
assertThat(bean1).isEqualTo(bean2);
228246
assertThat(bean2).isEqualTo(bean1);
@@ -232,7 +250,7 @@ private void assertEqualsHashCodeAndToString(ControllerAdviceBean bean1, Control
232250
}
233251

234252
private void assertOrder(Class<?> beanType, int expectedOrder) {
235-
assertThat(createControllerAdviceBean(beanType).getOrder()).isEqualTo(expectedOrder);
253+
assertThat(createSingletonControllerAdviceBean(beanType).getOrder()).isEqualTo(expectedOrder);
236254
}
237255

238256
private void assertApplicable(String message, ControllerAdviceBean controllerAdvice, Class<?> controllerBeanType) {

0 commit comments

Comments
 (0)