diff --git a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/condition/ConditionalOnBeanTests.java b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/condition/ConditionalOnBeanTests.java index defbd98597..8ac249f740 100644 --- a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/condition/ConditionalOnBeanTests.java +++ b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/condition/ConditionalOnBeanTests.java @@ -57,12 +57,7 @@ public class ConditionalOnBeanTests { this.context.register(FooConfiguration.class, OnBeanNameAndTypeConfiguration.class); this.context.refresh(); - /* - * Arguably this should be true, but as things are implemented the conditions - * specified in the different attributes of @ConditionalOnBean are combined with - * logical OR (not AND) so if any of them match the condition is true. - */ - assertThat(this.context.containsBean("bar")).isFalse(); + assertThat(this.context.containsBean("bar")).isTrue(); } @Test @@ -132,28 +127,34 @@ public class ConditionalOnBeanTests { @Configuration @ConditionalOnBean(name = "foo") protected static class OnBeanNameConfiguration { + @Bean public String bar() { return "bar"; } + } @Configuration - @ConditionalOnMissingBean(name = "foo", value = Date.class) + @ConditionalOnBean(name = "foo", value = Date.class) protected static class OnBeanNameAndTypeConfiguration { + @Bean public String bar() { return "bar"; } + } @Configuration @ConditionalOnBean(annotation = EnableScheduling.class) protected static class OnAnnotationConfiguration { + @Bean public String bar() { return "bar"; } + } @Configuration @@ -177,30 +178,36 @@ public class ConditionalOnBeanTests { @Configuration @ConditionalOnBean(type = "some.type.Missing") protected static class OnBeanMissingClassConfiguration { + @Bean public String bar() { return "bar"; } + } @Configuration @EnableScheduling protected static class FooConfiguration { + @Bean public String foo() { return "foo"; } + } @Configuration @ImportResource("org/springframework/boot/autoconfigure/condition/foo.xml") protected static class XmlConfiguration { + } @Configuration @ImportResource("org/springframework/boot/autoconfigure/condition/foo.xml") @Import(OnBeanNameConfiguration.class) protected static class CombinedXmlConfiguration { + } @Configuration diff --git a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/condition/ConditionalOnMissingBeanTests.java b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/condition/ConditionalOnMissingBeanTests.java index fe8f4dbd76..138bc924f9 100644 --- a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/condition/ConditionalOnMissingBeanTests.java +++ b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/condition/ConditionalOnMissingBeanTests.java @@ -16,6 +16,8 @@ package org.springframework.boot.autoconfigure.condition; +import java.util.Date; + import org.junit.Test; import org.springframework.beans.factory.FactoryBean; @@ -66,6 +68,19 @@ public class ConditionalOnMissingBeanTests { assertThat(this.context.getBean("foo")).isEqualTo("foo"); } + @Test + public void testNameAndTypeOnMissingBeanCondition() { + this.context.register(FooConfiguration.class, + OnBeanNameAndTypeConfiguration.class); + this.context.refresh(); + /* + * Arguably this should be true, but as things are implemented the conditions + * specified in the different attributes of @ConditionalOnBean are combined with + * logical OR (not AND) so if any of them match the condition is true. + */ + assertThat(this.context.containsBean("bar")).isFalse(); + } + @Test public void hierarchyConsidered() throws Exception { this.context.register(FooConfiguration.class); @@ -219,49 +234,72 @@ public class ConditionalOnMissingBeanTests { @Configuration @ConditionalOnMissingBean(name = "foo") protected static class OnBeanNameConfiguration { + + @Bean + public String bar() { + return "bar"; + } + + } + + @Configuration + @ConditionalOnMissingBean(name = "foo", value = Date.class) + @ConditionalOnBean(name = "foo", value = Date.class) + protected static class OnBeanNameAndTypeConfiguration { + @Bean public String bar() { return "bar"; } + } @Configuration protected static class FactoryBeanConfiguration { + @Bean public FactoryBean exampleBeanFactoryBean() { return new ExampleFactoryBean("foo"); } + } @Configuration protected static class FactoryBeanWithBeanMethodArgumentsConfiguration { + @Bean public FactoryBean exampleBeanFactoryBean( @Value("${theValue}") String value) { return new ExampleFactoryBean(value); } + } @Configuration protected static class ConcreteFactoryBeanConfiguration { + @Bean public ExampleFactoryBean exampleBeanFactoryBean() { return new ExampleFactoryBean("foo"); } + } @Configuration protected static class UnhelpfulFactoryBeanConfiguration { + @Bean @SuppressWarnings("rawtypes") public FactoryBean exampleBeanFactoryBean() { return new ExampleFactoryBean("foo"); } + } @Configuration @Import(NonspecificFactoryBeanClassAttributeRegistrar.class) protected static class NonspecificFactoryBeanClassAttributeConfiguration { + } protected static class NonspecificFactoryBeanClassAttributeRegistrar @@ -284,6 +322,7 @@ public class ConditionalOnMissingBeanTests { @Configuration @Import(NonspecificFactoryBeanClassAttributeRegistrar.class) protected static class NonspecificFactoryBeanStringAttributeConfiguration { + } protected static class NonspecificFactoryBeanStringAttributeRegistrar @@ -326,15 +365,18 @@ public class ConditionalOnMissingBeanTests { @Configuration @ImportResource("org/springframework/boot/autoconfigure/condition/factorybean.xml") protected static class FactoryBeanXmlConfiguration { + } @Configuration protected static class ConditionalOnFactoryBean { + @Bean @ConditionalOnMissingBean(ExampleBean.class) public ExampleBean createExampleBean() { return new ExampleBean("direct"); } + } @Configuration @@ -372,45 +414,55 @@ public class ConditionalOnMissingBeanTests { @Configuration @ConditionalOnMissingBean(annotation = EnableScheduling.class) protected static class OnAnnotationConfiguration { + @Bean public String bar() { return "bar"; } + } @Configuration @EnableScheduling protected static class FooConfiguration { + @Bean public String foo() { return "foo"; } + } @Configuration @ConditionalOnMissingBean(name = "foo") protected static class HierarchyConsidered { + @Bean public String bar() { return "bar"; } + } @Configuration @ConditionalOnMissingBean(name = "foo", search = SearchStrategy.CURRENT) protected static class HierarchyNotConsidered { + @Bean public String bar() { return "bar"; } + } @Configuration protected static class ExampleBeanConfiguration { + @Bean public ExampleBean exampleBean() { return new ExampleBean("test"); } + } @Configuration