From 65ef03a5bf62b2f3df5d7958151ddb9903821735 Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Fri, 25 Oct 2019 14:23:03 +0100 Subject: [PATCH] Remove nulls from description for bean defs with no res description Previously, if a bean definition had no resource description, the failure analysis description would state that the been had been defined in null which was of no use to the user. This commit updates the failure analysis to omit information about where the bean was definied when the definition has no resource description. Fixes gh-18721 --- ...BeanDefinitionOverrideFailureAnalyzer.java | 14 ++++--- ...efinitionOverrideFailureAnalyzerTests.java | 37 +++++++++++++++++++ 2 files changed, 46 insertions(+), 5 deletions(-) diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/diagnostics/analyzer/BeanDefinitionOverrideFailureAnalyzer.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/diagnostics/analyzer/BeanDefinitionOverrideFailureAnalyzer.java index 3664695ef9..ca536fcc9a 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/diagnostics/analyzer/BeanDefinitionOverrideFailureAnalyzer.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/diagnostics/analyzer/BeanDefinitionOverrideFailureAnalyzer.java @@ -42,11 +42,15 @@ class BeanDefinitionOverrideFailureAnalyzer extends AbstractFailureAnalyzer { + context.registerBean("testBean", String.class, (Supplier) String::new); + context.registerBean("testBean", String.class, (Supplier) String::new); + }); + String description = analysis.getDescription(); + assertThat(description) + .isEqualTo("The bean 'testBean' could not be registered. A bean with that name has already" + + " been defined and overriding is disabled."); + } + private FailureAnalysis performAnalysis(Class configuration) { BeanDefinitionOverrideException failure = createFailure(configuration); assertThat(failure).isNotNull(); @@ -63,6 +78,28 @@ public class BeanDefinitionOverrideFailureAnalyzerTests { } } + private FailureAnalysis performAnalysis( + ApplicationContextInitializer initializer) { + BeanDefinitionOverrideException failure = createFailure(initializer); + assertThat(failure).isNotNull(); + return new BeanDefinitionOverrideFailureAnalyzer().analyze(failure); + } + + private BeanDefinitionOverrideException createFailure( + ApplicationContextInitializer initializer) { + try { + AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(); + context.setAllowBeanDefinitionOverriding(false); + initializer.initialize(context); + context.refresh(); + context.close(); + return null; + } + catch (BeanDefinitionOverrideException ex) { + return ex; + } + } + @Configuration @Import({ FirstConfiguration.class, SecondConfiguration.class }) static class BeanOverrideConfiguration {