Move @AssertMissingBean to tests

Use @AssertMissingBean only for tests.
This commit is contained in:
Phillip Webb 2013-07-06 10:51:16 -07:00
parent d91e802139
commit a6341dc0af
4 changed files with 44 additions and 15 deletions

View File

@ -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 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: type in your application context, e.g. for the server properties:
@AssertMissingBean(ServerProperties.class)
@Bean @Bean
public ServerProperties serverProperties() { public ServerProperties serverProperties() {
ServerProperties server = new ServerProperties(); ServerProperties server = new ServerProperties();
@ -185,10 +184,6 @@ type in your application context, e.g. for the server properties:
return server; 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 ## Server Configuration
The `ServerProperties` are bound to application properties, and The `ServerProperties` are bound to application properties, and

View File

@ -18,15 +18,18 @@ package org.springframework.zero.autoconfigure.web;
import java.io.File; import java.io.File;
import org.hamcrest.Matchers;
import org.junit.After; import org.junit.After;
import org.junit.Before; import org.junit.Before;
import org.junit.Rule;
import org.junit.Test; import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.mockito.Mockito; import org.mockito.Mockito;
import org.springframework.beans.factory.NoUniqueBeanDefinitionException;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.zero.TestUtils; import org.springframework.zero.TestUtils;
import org.springframework.zero.autoconfigure.PropertyPlaceholderAutoConfiguration; 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.AnnotationConfigEmbeddedWebApplicationContext;
import org.springframework.zero.context.embedded.ConfigurableEmbeddedServletContainerFactory; import org.springframework.zero.context.embedded.ConfigurableEmbeddedServletContainerFactory;
import org.springframework.zero.context.embedded.EmbeddedServletContainerCustomizerBeanPostProcessor; import org.springframework.zero.context.embedded.EmbeddedServletContainerCustomizerBeanPostProcessor;
@ -42,16 +45,19 @@ import static org.junit.Assert.assertNotNull;
* *
* @author Dave Syer * @author Dave Syer
*/ */
public class ServerPropertiesConfigurationTests { public class ServerPropertiesAutoConfigurationTests {
private static ConfigurableEmbeddedServletContainerFactory containerFactory; private static ConfigurableEmbeddedServletContainerFactory containerFactory;
@Rule
public ExpectedException thrown = ExpectedException.none();
private AnnotationConfigEmbeddedWebApplicationContext context; private AnnotationConfigEmbeddedWebApplicationContext context;
@Before @Before
public void init() { public void init() {
containerFactory = Mockito containerFactory =
.mock(ConfigurableEmbeddedServletContainerFactory.class); Mockito.mock(ConfigurableEmbeddedServletContainerFactory.class);
} }
@After @After
@ -64,8 +70,8 @@ public class ServerPropertiesConfigurationTests {
@Test @Test
public void createFromConfigClass() throws Exception { public void createFromConfigClass() throws Exception {
this.context = new AnnotationConfigEmbeddedWebApplicationContext(); this.context = new AnnotationConfigEmbeddedWebApplicationContext();
this.context.register(Config.class, ServerPropertiesAutoConfiguration.class, this.context
PropertyPlaceholderAutoConfiguration.class); .register(Config.class, ServerPropertiesAutoConfiguration.class, PropertyPlaceholderAutoConfiguration.class);
TestUtils.addEnviroment(this.context, "server.port:9000"); TestUtils.addEnviroment(this.context, "server.port:9000");
this.context.refresh(); this.context.refresh();
ServerProperties server = this.context.getBean(ServerProperties.class); ServerProperties server = this.context.getBean(ServerProperties.class);
@ -78,8 +84,8 @@ public class ServerPropertiesConfigurationTests {
public void tomcatProperties() throws Exception { public void tomcatProperties() throws Exception {
containerFactory = Mockito.mock(TomcatEmbeddedServletContainerFactory.class); containerFactory = Mockito.mock(TomcatEmbeddedServletContainerFactory.class);
this.context = new AnnotationConfigEmbeddedWebApplicationContext(); this.context = new AnnotationConfigEmbeddedWebApplicationContext();
this.context.register(Config.class, ServerPropertiesAutoConfiguration.class, this.context
PropertyPlaceholderAutoConfiguration.class); .register(Config.class, ServerPropertiesAutoConfiguration.class, PropertyPlaceholderAutoConfiguration.class);
TestUtils.addEnviroment(this.context, "server.tomcat.basedir:target/foo"); TestUtils.addEnviroment(this.context, "server.tomcat.basedir:target/foo");
this.context.refresh(); this.context.refresh();
ServerProperties server = this.context.getBean(ServerProperties.class); ServerProperties server = this.context.getBean(ServerProperties.class);
@ -88,19 +94,45 @@ public class ServerPropertiesConfigurationTests {
Mockito.verify(containerFactory).setPort(8080); 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
.<Throwable> instanceOf(NoUniqueBeanDefinitionException.class));
this.context.refresh();
}
@Configuration @Configuration
protected static class Config { protected static class Config {
@Bean @Bean
public EmbeddedServletContainerFactory containerFactory() { public EmbeddedServletContainerFactory containerFactory() {
return ServerPropertiesConfigurationTests.containerFactory; return ServerPropertiesAutoConfigurationTests.containerFactory;
} }
@Bean @Bean
public EmbeddedServletContainerCustomizerBeanPostProcessor embeddedServletContainerCustomizerBeanPostProcessor() { public EmbeddedServletContainerCustomizerBeanPostProcessor
embeddedServletContainerCustomizerBeanPostProcessor() {
return new EmbeddedServletContainerCustomizerBeanPostProcessor(); return new EmbeddedServletContainerCustomizerBeanPostProcessor();
} }
} }
@Configuration
protected static class MutiServerPropertiesBeanConfig {
@Bean
public ServerProperties serverPropertiesOne() {
return new ServerProperties();
}
@Bean
public ServerProperties serverPropertiesTwo() {
return new ServerProperties();
}
}
} }

View File

@ -25,6 +25,7 @@ import java.lang.annotation.Target;
import org.springframework.beans.factory.BeanFactory; import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Conditional; 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 * {@link Conditional} that only matches when the specified bean classes and/or names are

View File

@ -22,6 +22,7 @@ import org.springframework.beans.factory.BeanCreationException;
import org.springframework.context.annotation.Condition; import org.springframework.context.annotation.Condition;
import org.springframework.context.annotation.ConditionContext; import org.springframework.context.annotation.ConditionContext;
import org.springframework.core.type.AnnotatedTypeMetadata; import org.springframework.core.type.AnnotatedTypeMetadata;
import org.springframework.zero.context.annotation.OnMissingBeanCondition;
/** /**
* {@link Condition} that checks that specific beans are missing. * {@link Condition} that checks that specific beans are missing.