Improve failure description when bean def has no resource description

Closes gh-33765
This commit is contained in:
Andy Wilkinson 2023-01-18 13:52:56 +00:00
parent c939e27925
commit b5cebed120
2 changed files with 11 additions and 11 deletions

View File

@ -1,5 +1,5 @@
/*
* Copyright 2012-2022 the original author or authors.
* Copyright 2012-2023 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.
@ -76,9 +76,14 @@ class NoUniqueBeanDefinitionFailureAnalyzer extends AbstractInjectionFailureAnal
private String getDefinitionDescription(String beanName, BeanDefinition definition) {
if (StringUtils.hasText(definition.getFactoryMethodName())) {
return String.format("\t- %s: defined by method '%s' in %s%n", beanName, definition.getFactoryMethodName(),
definition.getResourceDescription());
getResourceDescription(definition));
}
return String.format("\t- %s: defined in %s%n", beanName, definition.getResourceDescription());
return String.format("\t- %s: defined in %s%n", beanName, getResourceDescription(definition));
}
private String getResourceDescription(BeanDefinition definition) {
String resourceDescription = definition.getResourceDescription();
return (resourceDescription != null) ? resourceDescription : "unknown location";
}
private String[] extractBeanNames(NoUniqueBeanDefinitionException cause) {

View File

@ -1,5 +1,5 @@
/*
* Copyright 2012-2022 the original author or authors.
* Copyright 2012-2023 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.
@ -94,6 +94,7 @@ class NoUniqueBeanDefinitionFailureAnalyzerTests {
}
private BeanCreationException createFailure(Class<?> consumer) {
this.context.registerBean("beanOne", TestBean.class);
this.context.register(DuplicateBeansProducer.class, consumer);
this.context.setParent(new AnnotationConfigApplicationContext(ParentProducer.class));
try {
@ -110,8 +111,7 @@ class NoUniqueBeanDefinitionFailureAnalyzerTests {
}
private void assertFoundBeans(FailureAnalysis analysis) {
assertThat(analysis.getDescription())
.contains("beanOne: defined by method 'beanOne' in " + DuplicateBeansProducer.class.getName());
assertThat(analysis.getDescription()).contains("beanOne: defined in unknown location");
assertThat(analysis.getDescription())
.contains("beanTwo: defined by method 'beanTwo' in " + DuplicateBeansProducer.class.getName());
assertThat(analysis.getDescription())
@ -126,11 +126,6 @@ class NoUniqueBeanDefinitionFailureAnalyzerTests {
@ImportResource("/org/springframework/boot/diagnostics/analyzer/nounique/producer.xml")
static class DuplicateBeansProducer {
@Bean
TestBean beanOne() {
return new TestBean();
}
@Bean
TestBean beanTwo() {
return new TestBean();