Skip to content

Commit

Permalink
Polish "Fix condition source in OnBeanCondition"
Browse files Browse the repository at this point in the history
  • Loading branch information
snicoll committed Feb 11, 2020
1 parent 547fc30 commit 66809c6
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2019 the original author or authors.
* Copyright 2012-2020 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.
Expand Down Expand Up @@ -67,7 +67,6 @@
* @author Jakub Kubrynski
* @author Stephane Nicoll
* @author Andy Wilkinson
* @author Jorge Cordoba
* @see ConditionalOnBean
* @see ConditionalOnMissingBean
* @see ConditionalOnSingleCandidate
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.util.Collection;
import java.util.Date;
import java.util.function.Consumer;

Expand All @@ -29,6 +30,7 @@
import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.boot.autoconfigure.condition.ConditionEvaluationReport.ConditionAndOutcomes;
import org.springframework.boot.test.context.assertj.AssertableApplicationContext;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
import org.springframework.context.ConfigurableApplicationContext;
Expand Down Expand Up @@ -134,6 +136,17 @@ private void hasBarBean(AssertableApplicationContext context) {
assertThat(context.getBean("bar")).isEqualTo("bar");
}

@Test
void onBeanConditionOutputShouldNotContainConditionalOnMissingBeanClassInMessage() {
this.contextRunner.withUserConfiguration(OnBeanNameConfiguration.class).run((context) -> {
Collection<ConditionAndOutcomes> conditionAndOutcomes = ConditionEvaluationReport
.get(context.getSourceApplicationContext().getBeanFactory()).getConditionAndOutcomesBySource()
.values();
String message = conditionAndOutcomes.iterator().next().iterator().next().getOutcome().getMessage();
assertThat(message).doesNotContain("@ConditionalOnMissingBean");
});
}

@Test
void conditionEvaluationConsidersChangeInTypeWhenBeanIsOverridden() {
this.contextRunner.withUserConfiguration(OriginalDefinition.class, OverridingDefinition.class,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2019 the original author or authors.
* Copyright 2012-2020 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.
Expand Down Expand Up @@ -58,7 +58,6 @@
* @author Phillip Webb
* @author Jakub Kubrynski
* @author Andy Wilkinson
* @author Jorge Cordoba
*/
@SuppressWarnings("resource")
public class ConditionalOnMissingBeanTests {
Expand Down Expand Up @@ -137,14 +136,15 @@ void testAnnotationOnMissingBeanConditionWithEagerFactoryBean() {
assertThat(context.getBean("foo")).isEqualTo("foo");
});
}

@Test
void testOnMissingBeanConditionOutputShouldNotContainConditionalOnBeanClassInMessage() {
this.contextRunner.withUserConfiguration(ConditionalOnMissingBeanTests.OnBeanNameConfiguration.class).run((context) -> {
this.contextRunner.withUserConfiguration(OnBeanNameConfiguration.class).run((context) -> {
Collection<ConditionEvaluationReport.ConditionAndOutcomes> conditionAndOutcomes = ConditionEvaluationReport
.get(context.getSourceApplicationContext().getBeanFactory()).getConditionAndOutcomesBySource()
.values();
String message = conditionAndOutcomes.iterator().next().iterator().next().getOutcome().getMessage();
assertThat(message).doesNotContain("@ConditionalOnBean (names: foo; SearchStrategy: all) did not find any beans");
assertThat(message).doesNotContain("@ConditionalOnBean");
});
}

Expand Down

0 comments on commit 66809c6

Please sign in to comment.