mirror of
https://github.com/spring-projects/spring-boot.git
synced 2024-07-05 00:56:58 +08:00
Move @AssertMissingBean to tests
Use @AssertMissingBean only for tests.
This commit is contained in:
parent
d91e802139
commit
a6341dc0af
@ -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
|
||||
|
@ -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
|
||||
.<Throwable> 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();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
@ -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
|
@ -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.
|
Loading…
Reference in New Issue
Block a user