37
37
import org .springframework .context .annotation .Bean ;
38
38
import org .springframework .context .annotation .Configuration ;
39
39
import org .springframework .context .annotation .Import ;
40
+ import org .springframework .context .annotation .ImportResource ;
41
+ import org .springframework .stereotype .Component ;
40
42
import org .springframework .test .util .ReflectionTestUtils ;
41
43
import org .springframework .util .ClassUtils ;
44
+ import org .springframework .util .StringUtils ;
42
45
43
46
import static org .assertj .core .api .Assertions .assertThat ;
44
47
@@ -166,7 +169,8 @@ public void failureAnalysisForNullBeanByType() {
166
169
assertDescriptionConstructorMissingType (analysis , StringHandler .class , 0 ,
167
170
String .class );
168
171
assertUserDefinedBean (analysis , "as the bean value is null" ,
169
- TestNullBeanConfiguration .class , "string" , "string" , String .class );
172
+ ClassUtils .getShortName (TestNullBeanConfiguration .class ), "string" ,
173
+ "string" , String .class );
170
174
assertActionMissingType (analysis , String .class );
171
175
}
172
176
@@ -178,13 +182,37 @@ public void failureAnalysisForUnmatchedQualifier() {
178
182
"@org.springframework.beans.factory.annotation.Qualifier\\ (value=\" *alpha\" *\\ )" );
179
183
}
180
184
185
+ @ Test
186
+ public void failureAnalysisForQualifierBeanByType () {
187
+ FailureAnalysis analysis = analyzeFailure (
188
+ createFailure (TestClassQualifierComponentBeanConfiguration .class ));
189
+ assertDescriptionConstructorMissingType (analysis , TestClassQualifierHandler .class ,
190
+ 0 , TestClass .class );
191
+ assertThat (analysis .getDescription ())
192
+ .containsPattern ("Qualifier\\ (value=\" *test-class\" *\\ )" );
193
+ assertUserDefinedBean (analysis , null , null , null , "testClass" , TestClass .class );
194
+ assertActionMissingType (analysis , TestClass .class );
195
+ }
196
+
197
+ @ Test
198
+ public void failureAnalysisForQualifierBeanByTypeXmlResource () {
199
+ FailureAnalysis analysis = analyzeFailure (
200
+ createFailure (TestClassQualifierXmlBeanConfiguration .class ));
201
+ assertDescriptionConstructorMissingType (analysis , TestClassQualifierHandler .class ,
202
+ 0 , TestClass .class );
203
+ assertUserDefinedBean (analysis , null ,
204
+ "class path resource [org/springframework/boot/autoconfigure/diagnostics/analyzer/beans.xml]" ,
205
+ null , "xmlTestBean" , TestClass .class );
206
+ assertActionMissingType (analysis , TestClass .class );
207
+ }
208
+
181
209
private void assertDescriptionConstructorMissingType (FailureAnalysis analysis ,
182
210
Class <?> component , int index , Class <?> type ) {
183
211
String expected = String .format (
184
212
"Parameter %s of constructor in %s required a bean of "
185
213
+ "type '%s' that could not be found." ,
186
214
index , component .getName (), type .getName ());
187
- assertThat (analysis .getDescription ()).startsWith (expected );
215
+ assertThat (analysis .getDescription ()).contains (expected );
188
216
}
189
217
190
218
private void assertActionMissingType (FailureAnalysis analysis , Class <?> type ) {
@@ -218,13 +246,19 @@ private void assertClassDisabled(FailureAnalysis analysis, String description,
218
246
}
219
247
220
248
private void assertUserDefinedBean (FailureAnalysis analysis , String description ,
221
- Class <?> target , String methodName , String beanName , Class <?> beanClass ) {
222
- String expected = String .format (
223
- "User-defined bean '%s' of type '%s' : defined by method '%s' in %s" ,
224
- beanName , beanClass .getName (), methodName ,
225
- ClassUtils .getShortName (target ));
249
+ String target , String methodName , String beanName , Class <?> beanClass ) {
250
+ StringBuilder expected = new StringBuilder (String .format (
251
+ "User-defined bean '%s' of type '%s'" , beanName , beanClass .getName ()));
252
+ if (StringUtils .hasText (methodName )) {
253
+ expected .append (String .format (" : defined by method '%s'" , methodName ));
254
+ }
255
+ if (StringUtils .hasText (target )) {
256
+ expected .append (String .format (" in %s" , target ));
257
+ }
226
258
assertThat (analysis .getDescription ()).contains (expected );
227
- assertThat (analysis .getDescription ()).contains (description );
259
+ if (StringUtils .hasText (description )) {
260
+ assertThat (analysis .getDescription ()).contains (description );
261
+ }
228
262
}
229
263
230
264
private static void addExclusions (NoSuchBeanDefinitionFailureAnalyzer analyzer ,
@@ -295,6 +329,20 @@ protected static class StringMissingBeanNameConfiguration {
295
329
296
330
}
297
331
332
+ @ Configuration
333
+ @ Import ({ TestClass .class , TestClassQualifierHandler .class })
334
+ protected static class TestClassQualifierComponentBeanConfiguration {
335
+
336
+ }
337
+
338
+ @ Configuration
339
+ @ Import ({ TestClassQualifierHandler .class })
340
+ @ ImportResource ("classpath:/org/springframework/boot/autoconfigure/diagnostics"
341
+ + "/analyzer/beans.xml" )
342
+ protected static class TestClassQualifierXmlBeanConfiguration {
343
+
344
+ }
345
+
298
346
@ Configuration
299
347
@ ImportAutoConfiguration (TestNullBeanConfiguration .class )
300
348
@ Import (StringHandler .class )
@@ -392,4 +440,16 @@ public StringNameHandler(BeanFactory beanFactory) {
392
440
393
441
}
394
442
443
+ protected static class TestClassQualifierHandler {
444
+
445
+ public TestClassQualifierHandler (@ Qualifier ("test-class" ) TestClass foo ) {
446
+ }
447
+
448
+ }
449
+
450
+ @ Component ("testClass" )
451
+ protected static class TestClass {
452
+
453
+ }
454
+
395
455
}
0 commit comments