Improve BeanNotOfRequiredTypeFailureAnalyzer output
This commit modifies the output of BeanNotOfRequiredTypeFailureAnalyzer to include type information for both the actual and the required types and to remove ambiguity. Fixes gh-26821
This commit is contained in:
parent
ac8c6a6fb7
commit
5147fcacdf
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright 2012-2019 the original author or authors.
|
||||
* Copyright 2012-2021 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.
|
||||
|
@ -29,6 +29,7 @@ import org.springframework.boot.diagnostics.FailureAnalysis;
|
|||
* {@link BeanNotOfRequiredTypeException}.
|
||||
*
|
||||
* @author Andy Wilkinson
|
||||
* @author Scott Frederick
|
||||
* @since 1.4.0
|
||||
*/
|
||||
public class BeanNotOfRequiredTypeFailureAnalyzer extends AbstractFailureAnalyzer<BeanNotOfRequiredTypeException> {
|
||||
|
@ -48,8 +49,12 @@ public class BeanNotOfRequiredTypeFailureAnalyzer extends AbstractFailureAnalyze
|
|||
private String getDescription(BeanNotOfRequiredTypeException ex) {
|
||||
StringWriter description = new StringWriter();
|
||||
PrintWriter printer = new PrintWriter(description);
|
||||
printer.printf("The bean '%s' could not be injected as a '%s' because it is a "
|
||||
+ "JDK dynamic proxy that implements:%n", ex.getBeanName(), ex.getRequiredType().getName());
|
||||
printer.printf("The bean '%s' could not be injected because it is a JDK dynamic proxy%n%n", ex.getBeanName());
|
||||
printer.printf("The bean is of type '%s' and implements:%n", ex.getActualType().getName());
|
||||
for (Class<?> actualTypeInterface : ex.getActualType().getInterfaces()) {
|
||||
printer.println("\t" + actualTypeInterface.getName());
|
||||
}
|
||||
printer.printf("%nExpected a bean of type '%s' which implements:%n", ex.getRequiredType().getName());
|
||||
for (Class<?> requiredTypeInterface : ex.getRequiredType().getInterfaces()) {
|
||||
printer.println("\t" + requiredTypeInterface.getName());
|
||||
}
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright 2012-2019 the original author or authors.
|
||||
* Copyright 2012-2021 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.
|
||||
|
@ -35,6 +35,7 @@ import static org.junit.jupiter.api.Assertions.fail;
|
|||
* Tests for {@link BeanNotOfRequiredTypeFailureAnalyzer}.
|
||||
*
|
||||
* @author Andy Wilkinson
|
||||
* @author Scott Frederick
|
||||
*/
|
||||
class BeanNotOfRequiredTypeFailureAnalyzerTests {
|
||||
|
||||
|
@ -44,8 +45,11 @@ class BeanNotOfRequiredTypeFailureAnalyzerTests {
|
|||
void jdkProxyCausesInjectionFailure() {
|
||||
FailureAnalysis analysis = performAnalysis(JdkProxyConfiguration.class);
|
||||
assertThat(analysis.getDescription()).startsWith("The bean 'asyncBean'");
|
||||
assertThat(analysis.getDescription()).contains("'" + AsyncBean.class.getName() + "'");
|
||||
assertThat(analysis.getDescription()).endsWith(String.format("%s%n", SomeInterface.class.getName()));
|
||||
assertThat(analysis.getDescription())
|
||||
.containsPattern("The bean is of type '" + AsyncBean.class.getPackage().getName() + ".\\$Proxy.*'");
|
||||
assertThat(analysis.getDescription()).contains("and implements:\n\t" + SomeInterface.class.getName());
|
||||
assertThat(analysis.getDescription()).contains("Expected a bean of type '" + AsyncBean.class.getName() + "'");
|
||||
assertThat(analysis.getDescription()).contains("which implements:\n\t" + SomeInterface.class.getName());
|
||||
}
|
||||
|
||||
private FailureAnalysis performAnalysis(Class<?> configuration) {
|
||||
|
|
Loading…
Reference in New Issue