Merge branch '2.4.x' into 2.5.x

Closes gh-26935
pull/26949/head
Scott Frederick 3 years ago
commit 79f47b150a

@ -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…
Cancel
Save