Delete @SpringApplicationConfiguration
Remove the @SpringApplicationConfiguration annotation since it offers little value over @SpringApplicationTest. See gh-5562
This commit is contained in:
parent
4d404b214c
commit
b0b190b362
|
@ -550,7 +550,6 @@ example:
|
|||
[source,java,indent=0,subs="verbatim,quotes,attributes"]
|
||||
----
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@SpringApplicationConfiguration(SampleDataJpaApplication.class)
|
||||
@SpringApplicationTest(webEnvironment=WebEnvironment.RANDOM_PORT)
|
||||
public class MyWebIntegrationTests {
|
||||
|
||||
|
|
|
@ -23,6 +23,8 @@ import java.lang.annotation.Retention;
|
|||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
import org.springframework.boot.test.context.SpringApplicationContextLoader;
|
||||
import org.springframework.boot.test.context.SpringApplicationTest;
|
||||
import org.springframework.context.ApplicationContextInitializer;
|
||||
import org.springframework.context.ConfigurableApplicationContext;
|
||||
import org.springframework.core.annotation.AliasFor;
|
||||
|
@ -39,8 +41,8 @@ import org.springframework.test.context.ContextConfiguration;
|
|||
* @author Sam Brannen
|
||||
* @see SpringApplicationContextLoader
|
||||
* @see ContextConfiguration
|
||||
* @deprecated since 1.4.0 in favor of
|
||||
* {@link org.springframework.boot.test.context.SpringApplicationConfiguration}
|
||||
* @deprecated since 1.4.0 in favor of {@link SpringApplicationTest} or direct use of
|
||||
* {@link SpringApplicationContextLoader}.
|
||||
*/
|
||||
@ContextConfiguration(loader = SpringApplicationContextLoader.class)
|
||||
@Documented
|
||||
|
|
|
@ -1,112 +0,0 @@
|
|||
/*
|
||||
* Copyright 2012-2016 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.boot.test.context;
|
||||
|
||||
import java.lang.annotation.Documented;
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Inherited;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
import org.springframework.context.ApplicationContextInitializer;
|
||||
import org.springframework.context.ConfigurableApplicationContext;
|
||||
import org.springframework.core.annotation.AliasFor;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
|
||||
/**
|
||||
* Class-level annotation that is used to determine how to load and configure an
|
||||
* {@code ApplicationContext} for integration tests.
|
||||
* <p>
|
||||
* Similar to the standard {@link ContextConfiguration @ContextConfiguration} but uses
|
||||
* Spring Boot's {@link SpringApplicationContextLoader}.
|
||||
* <p>
|
||||
* Tests that using this annotation only to define {@code classes} should consider using
|
||||
* {@link SpringApplicationTest @SpringApplicationTest} instead.
|
||||
*
|
||||
* @author Dave Syer
|
||||
* @author Sam Brannen
|
||||
* @author Phillip Webb
|
||||
* @since 1.4.0
|
||||
* @see SpringApplicationTest
|
||||
* @see SpringApplicationContextLoader
|
||||
* @see ContextConfiguration
|
||||
*/
|
||||
@ContextConfiguration(loader = SpringApplicationContextLoader.class)
|
||||
@Documented
|
||||
@Inherited
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target(ElementType.TYPE)
|
||||
public @interface SpringApplicationConfiguration {
|
||||
|
||||
/**
|
||||
* The context configuration classes.
|
||||
* @see ContextConfiguration#classes()
|
||||
* @return the context configuration classes
|
||||
*/
|
||||
@AliasFor("classes")
|
||||
Class<?>[] value() default {};
|
||||
|
||||
/**
|
||||
* The context configuration locations.
|
||||
* @see ContextConfiguration#locations()
|
||||
* @return the context configuration locations
|
||||
*/
|
||||
@AliasFor(annotation = ContextConfiguration.class, attribute = "locations")
|
||||
String[] locations() default {};
|
||||
|
||||
/**
|
||||
* The context configuration classes.
|
||||
* @see ContextConfiguration#classes()
|
||||
* @return the context configuration classes
|
||||
*/
|
||||
@AliasFor("value")
|
||||
Class<?>[] classes() default {};
|
||||
|
||||
/**
|
||||
* The context configuration initializers.
|
||||
* @see ContextConfiguration#initializers()
|
||||
* @return the context configuration initializers
|
||||
*/
|
||||
@AliasFor(annotation = ContextConfiguration.class, attribute = "initializers")
|
||||
Class<? extends ApplicationContextInitializer<? extends ConfigurableApplicationContext>>[] initializers() default {};
|
||||
|
||||
/**
|
||||
* Should context locations be inherited.
|
||||
* @see ContextConfiguration#inheritLocations()
|
||||
* @return {@code true} if context locations should be inherited
|
||||
*/
|
||||
@AliasFor(annotation = ContextConfiguration.class, attribute = "inheritLocations")
|
||||
boolean inheritLocations() default true;
|
||||
|
||||
/**
|
||||
* Should initializers be inherited.
|
||||
* @see ContextConfiguration#inheritInitializers()
|
||||
* @return {@code true} if context initializers should be inherited
|
||||
*/
|
||||
@AliasFor(annotation = ContextConfiguration.class, attribute = "inheritInitializers")
|
||||
boolean inheritInitializers() default true;
|
||||
|
||||
/**
|
||||
* The name of the context hierarchy level.
|
||||
* @see ContextConfiguration#name()
|
||||
* @return the name of the context hierarchy level
|
||||
*/
|
||||
@AliasFor(annotation = ContextConfiguration.class, attribute = "name")
|
||||
String name() default "";
|
||||
|
||||
}
|
|
@ -24,35 +24,43 @@ import java.lang.annotation.RetentionPolicy;
|
|||
import java.lang.annotation.Target;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.SpringBootConfiguration;
|
||||
import org.springframework.boot.context.embedded.EmbeddedWebApplicationContext;
|
||||
import org.springframework.boot.context.web.LocalServerPort;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.core.annotation.AliasFor;
|
||||
import org.springframework.core.env.Environment;
|
||||
import org.springframework.test.context.BootstrapWith;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.ContextLoader;
|
||||
import org.springframework.web.context.WebApplicationContext;
|
||||
|
||||
/**
|
||||
* Test class annotation signifying that the tests are for a
|
||||
* {@link org.springframework.boot.SpringApplication Spring Boot Application}. By default
|
||||
* will load nested {@code @Configuration} classes, or fall back on
|
||||
* {@link SpringApplicationConfiguration @SpringApplicationConfiguration} search. Unless
|
||||
* otherwise configured, a {@link SpringApplicationContextLoader} will be used to load the
|
||||
* {@link ApplicationContext}. Use
|
||||
* {@link SpringApplicationConfiguration @SpringApplicationConfiguration} or
|
||||
* {@link ContextConfiguration @ContextConfiguration} if custom configuration is required.
|
||||
* <p>
|
||||
* The environment that will be used can be configured using the {@code mode} attribute.
|
||||
* By default, a mock servlet environment will be used when this annotation is used to a
|
||||
* test web application. If you want to start a real embedded servlet container in the
|
||||
* same way as a production application (listening on normal ports) configure
|
||||
* {@code mode=EMBEDDED_SERVLET}. If want to disable the creation of the mock servlet
|
||||
* environment, configure {@code mode=STANDARD}.
|
||||
* Annotation that can be specified on a test class that runs Spring Boot based tests.
|
||||
* Provides the following features over and above the regular
|
||||
* <em>Spring TestContext Framework</em>:
|
||||
* <ul>
|
||||
* <li>Uses {@link SpringApplicationContextLoader} as the default {@link ContextLoader}
|
||||
* when no specific
|
||||
* {@link ContextConfiguration#loader() @ContextConfiguration(loader=...)} is defined.
|
||||
* </li>
|
||||
* <li>Automatically searches for a
|
||||
* {@link SpringBootConfiguration @SpringBootConfiguration} when nested
|
||||
* {@code @Configuration} is not used, and no explicit {@link #classes() classes} are
|
||||
* specified.</li>
|
||||
* <li>Allows custom {@link Environment} properties to be defined using the
|
||||
* {@link #properties() properties attribute}.</li>
|
||||
* <li>Provides support for different {@link #webEnvironment() webEnvironment} modes,
|
||||
* including the ability to start a fully running container listening on a
|
||||
* {@link WebEnvironment#DEFINED_PORT defined} or {@link WebEnvironment#RANDOM_PORT
|
||||
* random} port.</li>
|
||||
* </ul>
|
||||
*
|
||||
* @author Phillip Webb
|
||||
* @author Andy Wilkinson
|
||||
* @since 1.4.0
|
||||
* @see ContextConfiguration
|
||||
*/
|
||||
@Target(ElementType.TYPE)
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
|
@ -76,6 +84,19 @@ public @interface SpringApplicationTest {
|
|||
@AliasFor("value")
|
||||
String[] properties() default {};
|
||||
|
||||
/**
|
||||
* The <em>annotated classes</em> to use for loading an
|
||||
* {@link org.springframework.context.ApplicationContext ApplicationContext}. Can also
|
||||
* be specified using
|
||||
* {@link ContextConfiguration#classes() @ContextConfiguration(classes=...)}. If no
|
||||
* explicit classes are defined the test will look for nested
|
||||
* {@link Configuration @Configuration} classes, before falling back to a
|
||||
* {@link SpringBootConfiguration} search.
|
||||
* @see ContextConfiguration#classes()
|
||||
* @return the annotated classes used to load the application context
|
||||
*/
|
||||
Class<?>[] classes() default {};
|
||||
|
||||
/**
|
||||
* The type of web environment to create when applicable. Defaults to
|
||||
* {@link WebEnvironment#MOCK}.
|
||||
|
|
|
@ -16,16 +16,20 @@
|
|||
|
||||
package org.springframework.boot.test.context;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.boot.test.context.SpringApplicationTest.WebEnvironment;
|
||||
import org.springframework.core.annotation.AnnotatedElementUtils;
|
||||
import org.springframework.test.context.ContextConfigurationAttributes;
|
||||
import org.springframework.test.context.ContextLoader;
|
||||
import org.springframework.test.context.MergedContextConfiguration;
|
||||
import org.springframework.test.context.TestContext;
|
||||
import org.springframework.test.context.TestContextBootstrapper;
|
||||
import org.springframework.test.context.web.WebMergedContextConfiguration;
|
||||
import org.springframework.util.ClassUtils;
|
||||
import org.springframework.util.ObjectUtils;
|
||||
|
||||
/**
|
||||
* {@link TestContextBootstrapper} for {@link SpringApplicationTest}.
|
||||
|
@ -55,6 +59,29 @@ class SpringApplicationTestContextBootstrapper extends SpringBootTestContextBoot
|
|||
return context;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected ContextLoader resolveContextLoader(Class<?> testClass,
|
||||
List<ContextConfigurationAttributes> configAttributesList) {
|
||||
SpringApplicationTest annotation = AnnotatedElementUtils
|
||||
.getMergedAnnotation(testClass, SpringApplicationTest.class);
|
||||
if (!ObjectUtils.isEmpty(annotation.classes())) {
|
||||
for (ContextConfigurationAttributes configAttributes : configAttributesList) {
|
||||
addConfigAttriubtesClasses(configAttributes, annotation.classes());
|
||||
}
|
||||
}
|
||||
return super.resolveContextLoader(testClass, configAttributesList);
|
||||
}
|
||||
|
||||
private void addConfigAttriubtesClasses(
|
||||
ContextConfigurationAttributes configAttributes, Class<?>[] classes) {
|
||||
List<Class<?>> combined = new ArrayList<Class<?>>();
|
||||
combined.addAll(Arrays.asList(classes));
|
||||
if (configAttributes.getClasses() != null) {
|
||||
combined.addAll(Arrays.asList(configAttributes.getClasses()));
|
||||
}
|
||||
configAttributes.setClasses(combined.toArray(new Class<?>[combined.size()]));
|
||||
}
|
||||
|
||||
@Override
|
||||
protected MergedContextConfiguration processMergedContextConfiguration(
|
||||
MergedContextConfiguration mergedConfig) {
|
||||
|
|
|
@ -56,15 +56,8 @@ public class SpringBootTestContextBootstrapper extends DefaultTestContextBootstr
|
|||
Class<?>[] classes = getOrFindConfigurationClasses(mergedConfig);
|
||||
List<String> propertySourceProperties = getAndProcessPropertySourceProperties(
|
||||
mergedConfig);
|
||||
return new MergedContextConfiguration(mergedConfig.getTestClass(),
|
||||
mergedConfig.getLocations(), classes,
|
||||
mergedConfig.getContextInitializerClasses(),
|
||||
mergedConfig.getActiveProfiles(),
|
||||
mergedConfig.getPropertySourceLocations(),
|
||||
propertySourceProperties
|
||||
.toArray(new String[propertySourceProperties.size()]),
|
||||
mergedConfig.getContextCustomizers(), mergedConfig.getContextLoader(),
|
||||
getCacheAwareContextLoaderDelegate(), mergedConfig.getParent());
|
||||
return createModifiedConfig(mergedConfig, classes, propertySourceProperties
|
||||
.toArray(new String[propertySourceProperties.size()]));
|
||||
}
|
||||
|
||||
protected Class<?>[] getOrFindConfigurationClasses(
|
||||
|
@ -135,4 +128,36 @@ public class SpringBootTestContextBootstrapper extends DefaultTestContextBootstr
|
|||
List<String> propertySourceProperties) {
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new {@link MergedContextConfiguration} with different classes.
|
||||
* @param mergedConfig the source config
|
||||
* @param classes the replacement classes
|
||||
* @return a new {@link MergedContextConfiguration}
|
||||
*/
|
||||
protected final MergedContextConfiguration createModifiedConfig(
|
||||
MergedContextConfiguration mergedConfig, Class<?>[] classes) {
|
||||
return createModifiedConfig(mergedConfig, classes,
|
||||
mergedConfig.getPropertySourceProperties());
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new {@link MergedContextConfiguration} with different classes and
|
||||
* properties.
|
||||
* @param mergedConfig the source config
|
||||
* @param classes the replacement classes
|
||||
* @param propertySourceProperties the replacement properties
|
||||
* @return a new {@link MergedContextConfiguration}
|
||||
*/
|
||||
protected final MergedContextConfiguration createModifiedConfig(
|
||||
MergedContextConfiguration mergedConfig, Class<?>[] classes,
|
||||
String[] propertySourceProperties) {
|
||||
return new MergedContextConfiguration(mergedConfig.getTestClass(),
|
||||
mergedConfig.getLocations(), classes,
|
||||
mergedConfig.getContextInitializerClasses(),
|
||||
mergedConfig.getActiveProfiles(),
|
||||
mergedConfig.getPropertySourceLocations(), propertySourceProperties,
|
||||
mergedConfig.getContextCustomizers(), mergedConfig.getContextLoader(),
|
||||
getCacheAwareContextLoaderDelegate(), mergedConfig.getParent());
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -23,9 +23,9 @@ import org.junit.Test;
|
|||
import org.junit.runner.RunWith;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringApplicationConfigurationMockMvcTests.Config;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.test.annotation.DirtiesContext;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
import org.springframework.test.context.web.WebAppConfiguration;
|
||||
import org.springframework.test.web.servlet.MockMvc;
|
||||
|
@ -48,9 +48,9 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.
|
|||
*/
|
||||
@RunWith(SpringRunner.class)
|
||||
@DirtiesContext
|
||||
@SpringApplicationConfiguration(classes = Config.class)
|
||||
@ContextConfiguration(loader = SpringApplicationContextLoader.class)
|
||||
@WebAppConfiguration
|
||||
public class SpringApplicationConfigurationMockMvcTests {
|
||||
public class SpringApplicationContextLoaderMockMvcTests {
|
||||
|
||||
@Autowired
|
||||
private WebApplicationContext context;
|
|
@ -29,16 +29,15 @@ import org.springframework.test.context.junit4.SpringRunner;
|
|||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
* Tests for {@link SpringApplicationContextLoader} with active profiles. See gh-1469.
|
||||
* Tests for {@link SpringApplicationTest} with active profiles. See gh-1469.
|
||||
*
|
||||
* @author Phillip Webb
|
||||
*/
|
||||
@RunWith(SpringRunner.class)
|
||||
@DirtiesContext
|
||||
@SpringApplicationConfiguration
|
||||
@SpringApplicationTest("spring.config.name=enableother")
|
||||
@ActiveProfiles("override")
|
||||
public class SpringApplicationConfigurationActiveProfileTests {
|
||||
public class SpringApplicationTestActiveProfileTests {
|
||||
|
||||
@Autowired
|
||||
private ApplicationContext context;
|
|
@ -27,14 +27,13 @@ import org.springframework.test.context.junit4.SpringRunner;
|
|||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
* Tests for {@link SpringApplicationContextLoader} (detectDefaultConfigurationClasses).
|
||||
* Tests for {@link SpringApplicationTest} (detectDefaultConfigurationClasses).
|
||||
*
|
||||
* @author Dave Syer
|
||||
*/
|
||||
@RunWith(SpringRunner.class)
|
||||
@DirtiesContext
|
||||
@SpringApplicationConfiguration
|
||||
public class SpringApplicationConfigurationDefaultConfigurationTests {
|
||||
public class SpringApplicationTestDefaultConfigurationTests {
|
||||
|
||||
@Autowired
|
||||
private Config config;
|
|
@ -21,19 +21,21 @@ import org.junit.runner.RunWith;
|
|||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.test.annotation.DirtiesContext;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
* Tests for {@link SpringApplicationContextLoader} (detectDefaultConfigurationClasses).
|
||||
* Tests for {@link SpringApplicationTest} (detectDefaultConfigurationClasses).
|
||||
*
|
||||
* @author Dave Syer
|
||||
*/
|
||||
@RunWith(SpringRunner.class)
|
||||
@DirtiesContext
|
||||
@SpringApplicationConfiguration(locations = "classpath:test.groovy")
|
||||
public class SpringApplicationConfigurationGroovyConfigurationTests {
|
||||
@SpringApplicationTest
|
||||
@ContextConfiguration(locations = "classpath:test.groovy")
|
||||
public class SpringApplicationTestGroovyConfigurationTests {
|
||||
|
||||
@Autowired
|
||||
private String foo;
|
|
@ -26,14 +26,14 @@ import org.springframework.test.context.junit4.SpringRunner;
|
|||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
* Tests for {@link SpringApplicationContextLoader} finding groovy config.
|
||||
* Tests for {@link SpringApplicationTest} finding groovy config.
|
||||
*
|
||||
* @author Phillip Webb
|
||||
*/
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringApplicationTest
|
||||
@DirtiesContext
|
||||
@SpringApplicationConfiguration
|
||||
public class SpringApplicationConfigurationGroovyConventionConfigurationTests {
|
||||
public class SpringApplicationTestGroovyConventionConfigurationTests {
|
||||
|
||||
@Autowired
|
||||
private String foo;
|
|
@ -20,7 +20,6 @@ import org.junit.Test;
|
|||
import org.junit.runner.RunWith;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.boot.test.context.SpringApplicationConfigurationJmxTests.Config;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
|
||||
|
@ -37,8 +36,7 @@ import static org.assertj.core.api.Assertions.assertThat;
|
|||
@RunWith(SpringRunner.class)
|
||||
@DirtiesContext
|
||||
@SpringApplicationTest
|
||||
@SpringApplicationConfiguration(Config.class)
|
||||
public class SpringApplicationConfigurationJmxTests {
|
||||
public class SpringApplicationTestJmxTests {
|
||||
|
||||
@Value("${spring.jmx.enabled}")
|
||||
private boolean jmx;
|
|
@ -20,22 +20,24 @@ import org.junit.Test;
|
|||
import org.junit.runner.RunWith;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringApplicationConfigurationMixedConfigurationTests.Config;
|
||||
import org.springframework.boot.test.context.SpringApplicationTestMixedConfigurationTests.Config;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.test.annotation.DirtiesContext;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
* Tests for {@link SpringApplicationContextLoader}.
|
||||
* Tests for {@link SpringApplicationTest}.
|
||||
*
|
||||
* @author Dave Syer
|
||||
*/
|
||||
@RunWith(SpringRunner.class)
|
||||
@DirtiesContext
|
||||
@SpringApplicationConfiguration(classes = Config.class, locations = "classpath:test.groovy")
|
||||
public class SpringApplicationConfigurationMixedConfigurationTests {
|
||||
@SpringApplicationTest
|
||||
@ContextConfiguration(classes = Config.class, locations = "classpath:test.groovy")
|
||||
public class SpringApplicationTestMixedConfigurationTests {
|
||||
|
||||
@Autowired
|
||||
private String foo;
|
|
@ -24,7 +24,6 @@ import org.junit.runner.RunWith;
|
|||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.boot.test.context.SpringApplicationTest.WebEnvironment;
|
||||
import org.springframework.boot.test.context.SpringApplicationTestPropertyLocationTests.Config;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
|
||||
|
@ -42,7 +41,6 @@ import static org.assertj.core.api.Assertions.assertThat;
|
|||
*/
|
||||
@RunWith(SpringRunner.class)
|
||||
@DirtiesContext
|
||||
@SpringApplicationConfiguration(Config.class)
|
||||
@SpringApplicationTest(webEnvironment = WebEnvironment.NONE, properties = "value1=123")
|
||||
@TestPropertySource(properties = "value2=456", locations = "classpath:/test-property-source-annotation.properties")
|
||||
public class SpringApplicationTestPropertyLocationTests {
|
||||
|
|
|
@ -0,0 +1,66 @@
|
|||
/*
|
||||
* Copyright 2012-2016 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.boot.test.context;
|
||||
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.rules.ExpectedException;
|
||||
import org.junit.runner.RunWith;
|
||||
|
||||
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.test.annotation.DirtiesContext;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
* Tests for {@link SpringApplicationTest} configured with specific classes.
|
||||
*
|
||||
* @author Phillip Webb
|
||||
*/
|
||||
@RunWith(SpringRunner.class)
|
||||
@DirtiesContext
|
||||
@SpringApplicationTest(classes = SpringApplicationTestWithClassesIntegrationTests.Config.class)
|
||||
public class SpringApplicationTestWithClassesIntegrationTests {
|
||||
|
||||
@Rule
|
||||
public ExpectedException thrown = ExpectedException.none();
|
||||
|
||||
@Autowired
|
||||
private ApplicationContext context;
|
||||
|
||||
@Test
|
||||
public void injectsOnlyConfig() throws Exception {
|
||||
assertThat(this.context.getBean(Config.class)).isNotNull();
|
||||
this.thrown.expect(NoSuchBeanDefinitionException.class);
|
||||
this.context.getBean(AdditionalConfig.class);
|
||||
}
|
||||
|
||||
@Configuration
|
||||
static class Config {
|
||||
|
||||
}
|
||||
|
||||
@Configuration
|
||||
static class AdditionalConfig {
|
||||
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,68 @@
|
|||
/*
|
||||
* Copyright 2012-2016 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.boot.test.context;
|
||||
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.rules.ExpectedException;
|
||||
import org.junit.runner.RunWith;
|
||||
|
||||
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.test.annotation.DirtiesContext;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
* Tests for {@link SpringApplicationTest} configured with {@link ContextConfiguration}.
|
||||
*
|
||||
* @author Phillip Webb
|
||||
*/
|
||||
@RunWith(SpringRunner.class)
|
||||
@DirtiesContext
|
||||
@SpringApplicationTest
|
||||
@ContextConfiguration(classes = SpringApplicationTestWithContextConfigurationIntegrationTests.Config.class)
|
||||
public class SpringApplicationTestWithContextConfigurationIntegrationTests {
|
||||
|
||||
@Rule
|
||||
public ExpectedException thrown = ExpectedException.none();
|
||||
|
||||
@Autowired
|
||||
private ApplicationContext context;
|
||||
|
||||
@Test
|
||||
public void injectsOnlyConfig() throws Exception {
|
||||
assertThat(this.context.getBean(Config.class)).isNotNull();
|
||||
this.thrown.expect(NoSuchBeanDefinitionException.class);
|
||||
this.context.getBean(AdditionalConfig.class);
|
||||
}
|
||||
|
||||
@Configuration
|
||||
static class Config {
|
||||
|
||||
}
|
||||
|
||||
@Configuration
|
||||
static class AdditionalConfig {
|
||||
|
||||
}
|
||||
|
||||
}
|
|
@ -26,14 +26,14 @@ import org.springframework.test.context.junit4.SpringRunner;
|
|||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
* Tests for {@link SpringApplicationContextLoader} finding XML config.
|
||||
* Tests for {@link SpringApplicationTest} finding XML config.
|
||||
*
|
||||
* @author Phillip Webb
|
||||
*/
|
||||
@RunWith(SpringRunner.class)
|
||||
@DirtiesContext
|
||||
@SpringApplicationConfiguration
|
||||
public class SpringApplicationConfigurationXmlConventionConfigurationTests {
|
||||
@SpringApplicationTest
|
||||
public class SpringApplicationTestXmlConventionConfigurationTests {
|
||||
|
||||
@Autowired
|
||||
private String foo;
|
|
@ -27,10 +27,10 @@ import javax.servlet.ServletContext;
|
|||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
|
||||
import org.springframework.boot.test.context.SpringApplicationConfiguration;
|
||||
import org.springframework.boot.test.mock.web.SpringBootMockServletContextTests.Config;
|
||||
import org.springframework.boot.test.context.SpringApplicationContextLoader;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.test.annotation.DirtiesContext;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
import org.springframework.test.context.web.WebAppConfiguration;
|
||||
import org.springframework.web.context.ServletContextAware;
|
||||
|
@ -45,7 +45,7 @@ import static org.hamcrest.Matchers.nullValue;
|
|||
*/
|
||||
@RunWith(SpringRunner.class)
|
||||
@DirtiesContext
|
||||
@SpringApplicationConfiguration(Config.class)
|
||||
@ContextConfiguration(loader = SpringApplicationContextLoader.class)
|
||||
@WebAppConfiguration("src/test/webapp")
|
||||
public class SpringBootMockServletContextTests implements ServletContextAware {
|
||||
|
||||
|
|
Loading…
Reference in New Issue