Skip to content

Commit beb5766

Browse files
committed
polish and add more tests
1 parent 3e3b264 commit beb5766

File tree

3 files changed

+87
-18
lines changed

3 files changed

+87
-18
lines changed

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/diagnostics/analyzer/AbstractNoSuchBeanDefinitionFailureAnalyzer.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -100,16 +100,6 @@ protected final FailureAnalysis analyze(Throwable rootFailure, T cause,
100100
beanMetadata);
101101
}
102102

103-
private List<Annotation> findInjectionAnnotations(Throwable failure) {
104-
UnsatisfiedDependencyException unsatisfiedDependencyException = findCause(failure,
105-
UnsatisfiedDependencyException.class);
106-
if (unsatisfiedDependencyException == null) {
107-
return Collections.emptyList();
108-
}
109-
return Arrays.asList(
110-
unsatisfiedDependencyException.getInjectionPoint().getAnnotations());
111-
}
112-
113103
/**
114104
* Returns an analysis of the given {@code rootFailure}, or {@code null} if no
115105
* analysis was possible.
@@ -144,6 +134,16 @@ private List<AutoConfigurationReport> getAutoConfigurationReports(
144134
return result;
145135
}
146136

137+
private List<Annotation> findInjectionAnnotations(Throwable failure) {
138+
UnsatisfiedDependencyException unsatisfiedDependencyException = findCause(failure,
139+
UnsatisfiedDependencyException.class);
140+
if (unsatisfiedDependencyException == null) {
141+
return Collections.emptyList();
142+
}
143+
return Arrays.asList(
144+
unsatisfiedDependencyException.getInjectionPoint().getAnnotations());
145+
}
146+
147147
private List<AutoConfigurationReport> getReportedConditionOutcomes(
148148
BeanMetadata beanMetadata) {
149149
List<AutoConfigurationReport> result = new ArrayList<>();

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/diagnostics/analyzer/NoSuchBeanDefinitionFailureAnalyzerTests.java

Lines changed: 68 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,11 @@
3737
import org.springframework.context.annotation.Bean;
3838
import org.springframework.context.annotation.Configuration;
3939
import org.springframework.context.annotation.Import;
40+
import org.springframework.context.annotation.ImportResource;
41+
import org.springframework.stereotype.Component;
4042
import org.springframework.test.util.ReflectionTestUtils;
4143
import org.springframework.util.ClassUtils;
44+
import org.springframework.util.StringUtils;
4245

4346
import static org.assertj.core.api.Assertions.assertThat;
4447

@@ -166,7 +169,8 @@ public void failureAnalysisForNullBeanByType() {
166169
assertDescriptionConstructorMissingType(analysis, StringHandler.class, 0,
167170
String.class);
168171
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);
170174
assertActionMissingType(analysis, String.class);
171175
}
172176

@@ -178,13 +182,37 @@ public void failureAnalysisForUnmatchedQualifier() {
178182
"@org.springframework.beans.factory.annotation.Qualifier\\(value=\"*alpha\"*\\)");
179183
}
180184

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+
181209
private void assertDescriptionConstructorMissingType(FailureAnalysis analysis,
182210
Class<?> component, int index, Class<?> type) {
183211
String expected = String.format(
184212
"Parameter %s of constructor in %s required a bean of "
185213
+ "type '%s' that could not be found.",
186214
index, component.getName(), type.getName());
187-
assertThat(analysis.getDescription()).startsWith(expected);
215+
assertThat(analysis.getDescription()).contains(expected);
188216
}
189217

190218
private void assertActionMissingType(FailureAnalysis analysis, Class<?> type) {
@@ -218,13 +246,19 @@ private void assertClassDisabled(FailureAnalysis analysis, String description,
218246
}
219247

220248
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+
}
226258
assertThat(analysis.getDescription()).contains(expected);
227-
assertThat(analysis.getDescription()).contains(description);
259+
if (StringUtils.hasText(description)) {
260+
assertThat(analysis.getDescription()).contains(description);
261+
}
228262
}
229263

230264
private static void addExclusions(NoSuchBeanDefinitionFailureAnalyzer analyzer,
@@ -295,6 +329,20 @@ protected static class StringMissingBeanNameConfiguration {
295329

296330
}
297331

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+
298346
@Configuration
299347
@ImportAutoConfiguration(TestNullBeanConfiguration.class)
300348
@Import(StringHandler.class)
@@ -392,4 +440,16 @@ public StringNameHandler(BeanFactory beanFactory) {
392440

393441
}
394442

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+
395455
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<beans xmlns="http://www.springframework.org/schema/beans"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
5+
6+
<bean class="org.springframework.boot.autoconfigure.diagnostics.analyzer.NoSuchBeanDefinitionFailureAnalyzerTests$TestClass"
7+
name="xmlTestBean"/>
8+
9+
</beans>

0 commit comments

Comments
 (0)