From a6341dc0af8fccb98c084d84efa12fc12586365d Mon Sep 17 00:00:00 2001 From: Phillip Webb Date: Sat, 6 Jul 2013 10:51:16 -0700 Subject: [PATCH] Move @AssertMissingBean to tests Use @AssertMissingBean only for tests. --- spring-zero-actuator/docs/Features.md | 5 -- ...rverPropertiesAutoConfigurationTests.java} | 52 +++++++++++++++---- .../context/annotation/AssertMissingBean.java | 1 + .../AssertMissingBeanCondition.java | 1 + 4 files changed, 44 insertions(+), 15 deletions(-) rename spring-zero-autoconfigure/src/test/java/org/springframework/zero/autoconfigure/web/{ServerPropertiesConfigurationTests.java => ServerPropertiesAutoConfigurationTests.java} (67%) rename spring-zero-core/src/{main => test}/java/org/springframework/zero/context/annotation/AssertMissingBean.java (96%) rename spring-zero-core/src/{main => test}/java/org/springframework/zero/context/annotation/AssertMissingBeanCondition.java (95%) diff --git a/spring-zero-actuator/docs/Features.md b/spring-zero-actuator/docs/Features.md index f889d0253c5..e2aa542a012 100644 --- a/spring-zero-actuator/docs/Features.md +++ b/spring-zero-actuator/docs/Features.md @@ -177,7 +177,6 @@ to change the default values imperatively in Java, so get more control over the process. You can do this by declaring a bean of the same type in your application context, e.g. for the server properties: - @AssertMissingBean(ServerProperties.class) @Bean public ServerProperties serverProperties() { ServerProperties server = new ServerProperties(); @@ -185,10 +184,6 @@ type in your application context, e.g. for the server properties: return server; } -Note the use of `@AssertMissingBean` to guard against any mistakes -where the bean is already defined (and therefore might already have -been bound). - ## Server Configuration The `ServerProperties` are bound to application properties, and diff --git a/spring-zero-autoconfigure/src/test/java/org/springframework/zero/autoconfigure/web/ServerPropertiesConfigurationTests.java b/spring-zero-autoconfigure/src/test/java/org/springframework/zero/autoconfigure/web/ServerPropertiesAutoConfigurationTests.java similarity index 67% rename from spring-zero-autoconfigure/src/test/java/org/springframework/zero/autoconfigure/web/ServerPropertiesConfigurationTests.java rename to spring-zero-autoconfigure/src/test/java/org/springframework/zero/autoconfigure/web/ServerPropertiesAutoConfigurationTests.java index 713fc168c6f..16747a30c86 100644 --- a/spring-zero-autoconfigure/src/test/java/org/springframework/zero/autoconfigure/web/ServerPropertiesConfigurationTests.java +++ b/spring-zero-autoconfigure/src/test/java/org/springframework/zero/autoconfigure/web/ServerPropertiesAutoConfigurationTests.java @@ -18,15 +18,18 @@ package org.springframework.zero.autoconfigure.web; import java.io.File; +import org.hamcrest.Matchers; import org.junit.After; import org.junit.Before; +import org.junit.Rule; import org.junit.Test; +import org.junit.rules.ExpectedException; import org.mockito.Mockito; +import org.springframework.beans.factory.NoUniqueBeanDefinitionException; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.zero.TestUtils; import org.springframework.zero.autoconfigure.PropertyPlaceholderAutoConfiguration; -import org.springframework.zero.autoconfigure.web.ServerPropertiesAutoConfiguration; import org.springframework.zero.context.embedded.AnnotationConfigEmbeddedWebApplicationContext; import org.springframework.zero.context.embedded.ConfigurableEmbeddedServletContainerFactory; import org.springframework.zero.context.embedded.EmbeddedServletContainerCustomizerBeanPostProcessor; @@ -42,16 +45,19 @@ import static org.junit.Assert.assertNotNull; * * @author Dave Syer */ -public class ServerPropertiesConfigurationTests { +public class ServerPropertiesAutoConfigurationTests { private static ConfigurableEmbeddedServletContainerFactory containerFactory; + @Rule + public ExpectedException thrown = ExpectedException.none(); + private AnnotationConfigEmbeddedWebApplicationContext context; @Before public void init() { - containerFactory = Mockito - .mock(ConfigurableEmbeddedServletContainerFactory.class); + containerFactory = + Mockito.mock(ConfigurableEmbeddedServletContainerFactory.class); } @After @@ -64,8 +70,8 @@ public class ServerPropertiesConfigurationTests { @Test public void createFromConfigClass() throws Exception { this.context = new AnnotationConfigEmbeddedWebApplicationContext(); - this.context.register(Config.class, ServerPropertiesAutoConfiguration.class, - PropertyPlaceholderAutoConfiguration.class); + this.context + .register(Config.class, ServerPropertiesAutoConfiguration.class, PropertyPlaceholderAutoConfiguration.class); TestUtils.addEnviroment(this.context, "server.port:9000"); this.context.refresh(); ServerProperties server = this.context.getBean(ServerProperties.class); @@ -78,8 +84,8 @@ public class ServerPropertiesConfigurationTests { public void tomcatProperties() throws Exception { containerFactory = Mockito.mock(TomcatEmbeddedServletContainerFactory.class); this.context = new AnnotationConfigEmbeddedWebApplicationContext(); - this.context.register(Config.class, ServerPropertiesAutoConfiguration.class, - PropertyPlaceholderAutoConfiguration.class); + this.context + .register(Config.class, ServerPropertiesAutoConfiguration.class, PropertyPlaceholderAutoConfiguration.class); TestUtils.addEnviroment(this.context, "server.tomcat.basedir:target/foo"); this.context.refresh(); ServerProperties server = this.context.getBean(ServerProperties.class); @@ -88,19 +94,45 @@ public class ServerPropertiesConfigurationTests { Mockito.verify(containerFactory).setPort(8080); } + @Test + public void testAccidentalMultipleServerPropertiesBeans() throws Exception { + this.context = new AnnotationConfigEmbeddedWebApplicationContext(); + this.context + .register(Config.class, MutiServerPropertiesBeanConfig.class, ServerPropertiesAutoConfiguration.class, PropertyPlaceholderAutoConfiguration.class); + this.thrown.expectCause(Matchers + . instanceOf(NoUniqueBeanDefinitionException.class)); + this.context.refresh(); + } + @Configuration protected static class Config { @Bean public EmbeddedServletContainerFactory containerFactory() { - return ServerPropertiesConfigurationTests.containerFactory; + return ServerPropertiesAutoConfigurationTests.containerFactory; } @Bean - public EmbeddedServletContainerCustomizerBeanPostProcessor embeddedServletContainerCustomizerBeanPostProcessor() { + public EmbeddedServletContainerCustomizerBeanPostProcessor + embeddedServletContainerCustomizerBeanPostProcessor() { return new EmbeddedServletContainerCustomizerBeanPostProcessor(); } } + @Configuration + protected static class MutiServerPropertiesBeanConfig { + + @Bean + public ServerProperties serverPropertiesOne() { + return new ServerProperties(); + } + + @Bean + public ServerProperties serverPropertiesTwo() { + return new ServerProperties(); + } + + } + } diff --git a/spring-zero-core/src/main/java/org/springframework/zero/context/annotation/AssertMissingBean.java b/spring-zero-core/src/test/java/org/springframework/zero/context/annotation/AssertMissingBean.java similarity index 96% rename from spring-zero-core/src/main/java/org/springframework/zero/context/annotation/AssertMissingBean.java rename to spring-zero-core/src/test/java/org/springframework/zero/context/annotation/AssertMissingBean.java index c101c3d9007..33bba730f83 100644 --- a/spring-zero-core/src/main/java/org/springframework/zero/context/annotation/AssertMissingBean.java +++ b/spring-zero-core/src/test/java/org/springframework/zero/context/annotation/AssertMissingBean.java @@ -25,6 +25,7 @@ import java.lang.annotation.Target; import org.springframework.beans.factory.BeanFactory; import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.Conditional; +import org.springframework.zero.context.annotation.ConditionalOnMissingBean; /** * {@link Conditional} that only matches when the specified bean classes and/or names are diff --git a/spring-zero-core/src/main/java/org/springframework/zero/context/annotation/AssertMissingBeanCondition.java b/spring-zero-core/src/test/java/org/springframework/zero/context/annotation/AssertMissingBeanCondition.java similarity index 95% rename from spring-zero-core/src/main/java/org/springframework/zero/context/annotation/AssertMissingBeanCondition.java rename to spring-zero-core/src/test/java/org/springframework/zero/context/annotation/AssertMissingBeanCondition.java index 69e4e7b96b2..699f74271f9 100644 --- a/spring-zero-core/src/main/java/org/springframework/zero/context/annotation/AssertMissingBeanCondition.java +++ b/spring-zero-core/src/test/java/org/springframework/zero/context/annotation/AssertMissingBeanCondition.java @@ -22,6 +22,7 @@ import org.springframework.beans.factory.BeanCreationException; import org.springframework.context.annotation.Condition; import org.springframework.context.annotation.ConditionContext; import org.springframework.core.type.AnnotatedTypeMetadata; +import org.springframework.zero.context.annotation.OnMissingBeanCondition; /** * {@link Condition} that checks that specific beans are missing.