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"]
|
[source,java,indent=0,subs="verbatim,quotes,attributes"]
|
||||||
----
|
----
|
||||||
@RunWith(SpringJUnit4ClassRunner.class)
|
@RunWith(SpringJUnit4ClassRunner.class)
|
||||||
@SpringApplicationConfiguration(SampleDataJpaApplication.class)
|
|
||||||
@SpringApplicationTest(webEnvironment=WebEnvironment.RANDOM_PORT)
|
@SpringApplicationTest(webEnvironment=WebEnvironment.RANDOM_PORT)
|
||||||
public class MyWebIntegrationTests {
|
public class MyWebIntegrationTests {
|
||||||
|
|
||||||
|
|
|
@ -23,6 +23,8 @@ import java.lang.annotation.Retention;
|
||||||
import java.lang.annotation.RetentionPolicy;
|
import java.lang.annotation.RetentionPolicy;
|
||||||
import java.lang.annotation.Target;
|
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.ApplicationContextInitializer;
|
||||||
import org.springframework.context.ConfigurableApplicationContext;
|
import org.springframework.context.ConfigurableApplicationContext;
|
||||||
import org.springframework.core.annotation.AliasFor;
|
import org.springframework.core.annotation.AliasFor;
|
||||||
|
@ -39,8 +41,8 @@ import org.springframework.test.context.ContextConfiguration;
|
||||||
* @author Sam Brannen
|
* @author Sam Brannen
|
||||||
* @see SpringApplicationContextLoader
|
* @see SpringApplicationContextLoader
|
||||||
* @see ContextConfiguration
|
* @see ContextConfiguration
|
||||||
* @deprecated since 1.4.0 in favor of
|
* @deprecated since 1.4.0 in favor of {@link SpringApplicationTest} or direct use of
|
||||||
* {@link org.springframework.boot.test.context.SpringApplicationConfiguration}
|
* {@link SpringApplicationContextLoader}.
|
||||||
*/
|
*/
|
||||||
@ContextConfiguration(loader = SpringApplicationContextLoader.class)
|
@ContextConfiguration(loader = SpringApplicationContextLoader.class)
|
||||||
@Documented
|
@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 java.lang.annotation.Target;
|
||||||
|
|
||||||
import org.springframework.boot.SpringApplication;
|
import org.springframework.boot.SpringApplication;
|
||||||
|
import org.springframework.boot.SpringBootConfiguration;
|
||||||
import org.springframework.boot.context.embedded.EmbeddedWebApplicationContext;
|
import org.springframework.boot.context.embedded.EmbeddedWebApplicationContext;
|
||||||
import org.springframework.boot.context.web.LocalServerPort;
|
import org.springframework.boot.context.web.LocalServerPort;
|
||||||
import org.springframework.context.ApplicationContext;
|
import org.springframework.context.ApplicationContext;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
import org.springframework.core.annotation.AliasFor;
|
import org.springframework.core.annotation.AliasFor;
|
||||||
import org.springframework.core.env.Environment;
|
import org.springframework.core.env.Environment;
|
||||||
import org.springframework.test.context.BootstrapWith;
|
import org.springframework.test.context.BootstrapWith;
|
||||||
import org.springframework.test.context.ContextConfiguration;
|
import org.springframework.test.context.ContextConfiguration;
|
||||||
|
import org.springframework.test.context.ContextLoader;
|
||||||
import org.springframework.web.context.WebApplicationContext;
|
import org.springframework.web.context.WebApplicationContext;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Test class annotation signifying that the tests are for a
|
* Annotation that can be specified on a test class that runs Spring Boot based tests.
|
||||||
* {@link org.springframework.boot.SpringApplication Spring Boot Application}. By default
|
* Provides the following features over and above the regular
|
||||||
* will load nested {@code @Configuration} classes, or fall back on
|
* <em>Spring TestContext Framework</em>:
|
||||||
* {@link SpringApplicationConfiguration @SpringApplicationConfiguration} search. Unless
|
* <ul>
|
||||||
* otherwise configured, a {@link SpringApplicationContextLoader} will be used to load the
|
* <li>Uses {@link SpringApplicationContextLoader} as the default {@link ContextLoader}
|
||||||
* {@link ApplicationContext}. Use
|
* when no specific
|
||||||
* {@link SpringApplicationConfiguration @SpringApplicationConfiguration} or
|
* {@link ContextConfiguration#loader() @ContextConfiguration(loader=...)} is defined.
|
||||||
* {@link ContextConfiguration @ContextConfiguration} if custom configuration is required.
|
* </li>
|
||||||
* <p>
|
* <li>Automatically searches for a
|
||||||
* The environment that will be used can be configured using the {@code mode} attribute.
|
* {@link SpringBootConfiguration @SpringBootConfiguration} when nested
|
||||||
* By default, a mock servlet environment will be used when this annotation is used to a
|
* {@code @Configuration} is not used, and no explicit {@link #classes() classes} are
|
||||||
* test web application. If you want to start a real embedded servlet container in the
|
* specified.</li>
|
||||||
* same way as a production application (listening on normal ports) configure
|
* <li>Allows custom {@link Environment} properties to be defined using the
|
||||||
* {@code mode=EMBEDDED_SERVLET}. If want to disable the creation of the mock servlet
|
* {@link #properties() properties attribute}.</li>
|
||||||
* environment, configure {@code mode=STANDARD}.
|
* <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 Phillip Webb
|
||||||
* @author Andy Wilkinson
|
* @author Andy Wilkinson
|
||||||
* @since 1.4.0
|
* @since 1.4.0
|
||||||
|
* @see ContextConfiguration
|
||||||
*/
|
*/
|
||||||
@Target(ElementType.TYPE)
|
@Target(ElementType.TYPE)
|
||||||
@Retention(RetentionPolicy.RUNTIME)
|
@Retention(RetentionPolicy.RUNTIME)
|
||||||
|
@ -76,6 +84,19 @@ public @interface SpringApplicationTest {
|
||||||
@AliasFor("value")
|
@AliasFor("value")
|
||||||
String[] properties() default {};
|
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
|
* The type of web environment to create when applicable. Defaults to
|
||||||
* {@link WebEnvironment#MOCK}.
|
* {@link WebEnvironment#MOCK}.
|
||||||
|
|
|
@ -16,16 +16,20 @@
|
||||||
|
|
||||||
package org.springframework.boot.test.context;
|
package org.springframework.boot.test.context;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import org.springframework.boot.test.context.SpringApplicationTest.WebEnvironment;
|
import org.springframework.boot.test.context.SpringApplicationTest.WebEnvironment;
|
||||||
import org.springframework.core.annotation.AnnotatedElementUtils;
|
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.MergedContextConfiguration;
|
||||||
import org.springframework.test.context.TestContext;
|
import org.springframework.test.context.TestContext;
|
||||||
import org.springframework.test.context.TestContextBootstrapper;
|
import org.springframework.test.context.TestContextBootstrapper;
|
||||||
import org.springframework.test.context.web.WebMergedContextConfiguration;
|
import org.springframework.test.context.web.WebMergedContextConfiguration;
|
||||||
import org.springframework.util.ClassUtils;
|
import org.springframework.util.ClassUtils;
|
||||||
|
import org.springframework.util.ObjectUtils;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* {@link TestContextBootstrapper} for {@link SpringApplicationTest}.
|
* {@link TestContextBootstrapper} for {@link SpringApplicationTest}.
|
||||||
|
@ -55,6 +59,29 @@ class SpringApplicationTestContextBootstrapper extends SpringBootTestContextBoot
|
||||||
return context;
|
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
|
@Override
|
||||||
protected MergedContextConfiguration processMergedContextConfiguration(
|
protected MergedContextConfiguration processMergedContextConfiguration(
|
||||||
MergedContextConfiguration mergedConfig) {
|
MergedContextConfiguration mergedConfig) {
|
||||||
|
|
|
@ -56,15 +56,8 @@ public class SpringBootTestContextBootstrapper extends DefaultTestContextBootstr
|
||||||
Class<?>[] classes = getOrFindConfigurationClasses(mergedConfig);
|
Class<?>[] classes = getOrFindConfigurationClasses(mergedConfig);
|
||||||
List<String> propertySourceProperties = getAndProcessPropertySourceProperties(
|
List<String> propertySourceProperties = getAndProcessPropertySourceProperties(
|
||||||
mergedConfig);
|
mergedConfig);
|
||||||
return new MergedContextConfiguration(mergedConfig.getTestClass(),
|
return createModifiedConfig(mergedConfig, classes, propertySourceProperties
|
||||||
mergedConfig.getLocations(), classes,
|
.toArray(new String[propertySourceProperties.size()]));
|
||||||
mergedConfig.getContextInitializerClasses(),
|
|
||||||
mergedConfig.getActiveProfiles(),
|
|
||||||
mergedConfig.getPropertySourceLocations(),
|
|
||||||
propertySourceProperties
|
|
||||||
.toArray(new String[propertySourceProperties.size()]),
|
|
||||||
mergedConfig.getContextCustomizers(), mergedConfig.getContextLoader(),
|
|
||||||
getCacheAwareContextLoaderDelegate(), mergedConfig.getParent());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
protected Class<?>[] getOrFindConfigurationClasses(
|
protected Class<?>[] getOrFindConfigurationClasses(
|
||||||
|
@ -135,4 +128,36 @@ public class SpringBootTestContextBootstrapper extends DefaultTestContextBootstr
|
||||||
List<String> propertySourceProperties) {
|
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.junit.runner.RunWith;
|
||||||
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.boot.test.context.SpringApplicationConfigurationMockMvcTests.Config;
|
|
||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
import org.springframework.test.annotation.DirtiesContext;
|
import org.springframework.test.annotation.DirtiesContext;
|
||||||
|
import org.springframework.test.context.ContextConfiguration;
|
||||||
import org.springframework.test.context.junit4.SpringRunner;
|
import org.springframework.test.context.junit4.SpringRunner;
|
||||||
import org.springframework.test.context.web.WebAppConfiguration;
|
import org.springframework.test.context.web.WebAppConfiguration;
|
||||||
import org.springframework.test.web.servlet.MockMvc;
|
import org.springframework.test.web.servlet.MockMvc;
|
||||||
|
@ -48,9 +48,9 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.
|
||||||
*/
|
*/
|
||||||
@RunWith(SpringRunner.class)
|
@RunWith(SpringRunner.class)
|
||||||
@DirtiesContext
|
@DirtiesContext
|
||||||
@SpringApplicationConfiguration(classes = Config.class)
|
@ContextConfiguration(loader = SpringApplicationContextLoader.class)
|
||||||
@WebAppConfiguration
|
@WebAppConfiguration
|
||||||
public class SpringApplicationConfigurationMockMvcTests {
|
public class SpringApplicationContextLoaderMockMvcTests {
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private WebApplicationContext context;
|
private WebApplicationContext context;
|
|
@ -29,16 +29,15 @@ import org.springframework.test.context.junit4.SpringRunner;
|
||||||
import static org.assertj.core.api.Assertions.assertThat;
|
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
|
* @author Phillip Webb
|
||||||
*/
|
*/
|
||||||
@RunWith(SpringRunner.class)
|
@RunWith(SpringRunner.class)
|
||||||
@DirtiesContext
|
@DirtiesContext
|
||||||
@SpringApplicationConfiguration
|
|
||||||
@SpringApplicationTest("spring.config.name=enableother")
|
@SpringApplicationTest("spring.config.name=enableother")
|
||||||
@ActiveProfiles("override")
|
@ActiveProfiles("override")
|
||||||
public class SpringApplicationConfigurationActiveProfileTests {
|
public class SpringApplicationTestActiveProfileTests {
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private ApplicationContext context;
|
private ApplicationContext context;
|
|
@ -27,14 +27,13 @@ import org.springframework.test.context.junit4.SpringRunner;
|
||||||
import static org.assertj.core.api.Assertions.assertThat;
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Tests for {@link SpringApplicationContextLoader} (detectDefaultConfigurationClasses).
|
* Tests for {@link SpringApplicationTest} (detectDefaultConfigurationClasses).
|
||||||
*
|
*
|
||||||
* @author Dave Syer
|
* @author Dave Syer
|
||||||
*/
|
*/
|
||||||
@RunWith(SpringRunner.class)
|
@RunWith(SpringRunner.class)
|
||||||
@DirtiesContext
|
@DirtiesContext
|
||||||
@SpringApplicationConfiguration
|
public class SpringApplicationTestDefaultConfigurationTests {
|
||||||
public class SpringApplicationConfigurationDefaultConfigurationTests {
|
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private Config config;
|
private Config config;
|
|
@ -21,19 +21,21 @@ import org.junit.runner.RunWith;
|
||||||
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.test.annotation.DirtiesContext;
|
import org.springframework.test.annotation.DirtiesContext;
|
||||||
|
import org.springframework.test.context.ContextConfiguration;
|
||||||
import org.springframework.test.context.junit4.SpringRunner;
|
import org.springframework.test.context.junit4.SpringRunner;
|
||||||
|
|
||||||
import static org.assertj.core.api.Assertions.assertThat;
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Tests for {@link SpringApplicationContextLoader} (detectDefaultConfigurationClasses).
|
* Tests for {@link SpringApplicationTest} (detectDefaultConfigurationClasses).
|
||||||
*
|
*
|
||||||
* @author Dave Syer
|
* @author Dave Syer
|
||||||
*/
|
*/
|
||||||
@RunWith(SpringRunner.class)
|
@RunWith(SpringRunner.class)
|
||||||
@DirtiesContext
|
@DirtiesContext
|
||||||
@SpringApplicationConfiguration(locations = "classpath:test.groovy")
|
@SpringApplicationTest
|
||||||
public class SpringApplicationConfigurationGroovyConfigurationTests {
|
@ContextConfiguration(locations = "classpath:test.groovy")
|
||||||
|
public class SpringApplicationTestGroovyConfigurationTests {
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private String foo;
|
private String foo;
|
|
@ -26,14 +26,14 @@ import org.springframework.test.context.junit4.SpringRunner;
|
||||||
import static org.assertj.core.api.Assertions.assertThat;
|
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
|
* @author Phillip Webb
|
||||||
*/
|
*/
|
||||||
@RunWith(SpringRunner.class)
|
@RunWith(SpringRunner.class)
|
||||||
|
@SpringApplicationTest
|
||||||
@DirtiesContext
|
@DirtiesContext
|
||||||
@SpringApplicationConfiguration
|
public class SpringApplicationTestGroovyConventionConfigurationTests {
|
||||||
public class SpringApplicationConfigurationGroovyConventionConfigurationTests {
|
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private String foo;
|
private String foo;
|
|
@ -20,7 +20,6 @@ import org.junit.Test;
|
||||||
import org.junit.runner.RunWith;
|
import org.junit.runner.RunWith;
|
||||||
|
|
||||||
import org.springframework.beans.factory.annotation.Value;
|
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.Bean;
|
||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
|
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
|
||||||
|
@ -37,8 +36,7 @@ import static org.assertj.core.api.Assertions.assertThat;
|
||||||
@RunWith(SpringRunner.class)
|
@RunWith(SpringRunner.class)
|
||||||
@DirtiesContext
|
@DirtiesContext
|
||||||
@SpringApplicationTest
|
@SpringApplicationTest
|
||||||
@SpringApplicationConfiguration(Config.class)
|
public class SpringApplicationTestJmxTests {
|
||||||
public class SpringApplicationConfigurationJmxTests {
|
|
||||||
|
|
||||||
@Value("${spring.jmx.enabled}")
|
@Value("${spring.jmx.enabled}")
|
||||||
private boolean jmx;
|
private boolean jmx;
|
|
@ -20,22 +20,24 @@ import org.junit.Test;
|
||||||
import org.junit.runner.RunWith;
|
import org.junit.runner.RunWith;
|
||||||
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
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.context.annotation.Configuration;
|
||||||
import org.springframework.test.annotation.DirtiesContext;
|
import org.springframework.test.annotation.DirtiesContext;
|
||||||
|
import org.springframework.test.context.ContextConfiguration;
|
||||||
import org.springframework.test.context.junit4.SpringRunner;
|
import org.springframework.test.context.junit4.SpringRunner;
|
||||||
|
|
||||||
import static org.assertj.core.api.Assertions.assertThat;
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Tests for {@link SpringApplicationContextLoader}.
|
* Tests for {@link SpringApplicationTest}.
|
||||||
*
|
*
|
||||||
* @author Dave Syer
|
* @author Dave Syer
|
||||||
*/
|
*/
|
||||||
@RunWith(SpringRunner.class)
|
@RunWith(SpringRunner.class)
|
||||||
@DirtiesContext
|
@DirtiesContext
|
||||||
@SpringApplicationConfiguration(classes = Config.class, locations = "classpath:test.groovy")
|
@SpringApplicationTest
|
||||||
public class SpringApplicationConfigurationMixedConfigurationTests {
|
@ContextConfiguration(classes = Config.class, locations = "classpath:test.groovy")
|
||||||
|
public class SpringApplicationTestMixedConfigurationTests {
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private String foo;
|
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.Autowired;
|
||||||
import org.springframework.beans.factory.annotation.Value;
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
import org.springframework.boot.test.context.SpringApplicationTest.WebEnvironment;
|
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.Bean;
|
||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
|
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
|
||||||
|
@ -42,7 +41,6 @@ import static org.assertj.core.api.Assertions.assertThat;
|
||||||
*/
|
*/
|
||||||
@RunWith(SpringRunner.class)
|
@RunWith(SpringRunner.class)
|
||||||
@DirtiesContext
|
@DirtiesContext
|
||||||
@SpringApplicationConfiguration(Config.class)
|
|
||||||
@SpringApplicationTest(webEnvironment = WebEnvironment.NONE, properties = "value1=123")
|
@SpringApplicationTest(webEnvironment = WebEnvironment.NONE, properties = "value1=123")
|
||||||
@TestPropertySource(properties = "value2=456", locations = "classpath:/test-property-source-annotation.properties")
|
@TestPropertySource(properties = "value2=456", locations = "classpath:/test-property-source-annotation.properties")
|
||||||
public class SpringApplicationTestPropertyLocationTests {
|
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;
|
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
|
* @author Phillip Webb
|
||||||
*/
|
*/
|
||||||
@RunWith(SpringRunner.class)
|
@RunWith(SpringRunner.class)
|
||||||
@DirtiesContext
|
@DirtiesContext
|
||||||
@SpringApplicationConfiguration
|
@SpringApplicationTest
|
||||||
public class SpringApplicationConfigurationXmlConventionConfigurationTests {
|
public class SpringApplicationTestXmlConventionConfigurationTests {
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private String foo;
|
private String foo;
|
|
@ -27,10 +27,10 @@ import javax.servlet.ServletContext;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.junit.runner.RunWith;
|
import org.junit.runner.RunWith;
|
||||||
|
|
||||||
import org.springframework.boot.test.context.SpringApplicationConfiguration;
|
import org.springframework.boot.test.context.SpringApplicationContextLoader;
|
||||||
import org.springframework.boot.test.mock.web.SpringBootMockServletContextTests.Config;
|
|
||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
import org.springframework.test.annotation.DirtiesContext;
|
import org.springframework.test.annotation.DirtiesContext;
|
||||||
|
import org.springframework.test.context.ContextConfiguration;
|
||||||
import org.springframework.test.context.junit4.SpringRunner;
|
import org.springframework.test.context.junit4.SpringRunner;
|
||||||
import org.springframework.test.context.web.WebAppConfiguration;
|
import org.springframework.test.context.web.WebAppConfiguration;
|
||||||
import org.springframework.web.context.ServletContextAware;
|
import org.springframework.web.context.ServletContextAware;
|
||||||
|
@ -45,7 +45,7 @@ import static org.hamcrest.Matchers.nullValue;
|
||||||
*/
|
*/
|
||||||
@RunWith(SpringRunner.class)
|
@RunWith(SpringRunner.class)
|
||||||
@DirtiesContext
|
@DirtiesContext
|
||||||
@SpringApplicationConfiguration(Config.class)
|
@ContextConfiguration(loader = SpringApplicationContextLoader.class)
|
||||||
@WebAppConfiguration("src/test/webapp")
|
@WebAppConfiguration("src/test/webapp")
|
||||||
public class SpringBootMockServletContextTests implements ServletContextAware {
|
public class SpringBootMockServletContextTests implements ServletContextAware {
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue