diff --git a/spring-boot-docs/src/main/asciidoc/howto.adoc b/spring-boot-docs/src/main/asciidoc/howto.adoc index c8783d161f5..3e08e8a5e93 100644 --- a/spring-boot-docs/src/main/asciidoc/howto.adoc +++ b/spring-boot-docs/src/main/asciidoc/howto.adoc @@ -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 { diff --git a/spring-boot-test/src/main/java/org/springframework/boot/test/SpringApplicationConfiguration.java b/spring-boot-test/src/main/java/org/springframework/boot/test/SpringApplicationConfiguration.java index 27f170614b6..409ec648cb4 100644 --- a/spring-boot-test/src/main/java/org/springframework/boot/test/SpringApplicationConfiguration.java +++ b/spring-boot-test/src/main/java/org/springframework/boot/test/SpringApplicationConfiguration.java @@ -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 diff --git a/spring-boot-test/src/main/java/org/springframework/boot/test/context/SpringApplicationConfiguration.java b/spring-boot-test/src/main/java/org/springframework/boot/test/context/SpringApplicationConfiguration.java deleted file mode 100644 index efde57446bf..00000000000 --- a/spring-boot-test/src/main/java/org/springframework/boot/test/context/SpringApplicationConfiguration.java +++ /dev/null @@ -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. - *

- * Similar to the standard {@link ContextConfiguration @ContextConfiguration} but uses - * Spring Boot's {@link SpringApplicationContextLoader}. - *

- * 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>[] 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 ""; - -} diff --git a/spring-boot-test/src/main/java/org/springframework/boot/test/context/SpringApplicationTest.java b/spring-boot-test/src/main/java/org/springframework/boot/test/context/SpringApplicationTest.java index 7b9d4e6c9b0..1749a4ef5d9 100644 --- a/spring-boot-test/src/main/java/org/springframework/boot/test/context/SpringApplicationTest.java +++ b/spring-boot-test/src/main/java/org/springframework/boot/test/context/SpringApplicationTest.java @@ -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. - *

- * 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 + * Spring TestContext Framework: + *

* * @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 annotated classes 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}. diff --git a/spring-boot-test/src/main/java/org/springframework/boot/test/context/SpringApplicationTestContextBootstrapper.java b/spring-boot-test/src/main/java/org/springframework/boot/test/context/SpringApplicationTestContextBootstrapper.java index f276b4d9610..a94a974e41d 100644 --- a/spring-boot-test/src/main/java/org/springframework/boot/test/context/SpringApplicationTestContextBootstrapper.java +++ b/spring-boot-test/src/main/java/org/springframework/boot/test/context/SpringApplicationTestContextBootstrapper.java @@ -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 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> combined = new ArrayList>(); + 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) { diff --git a/spring-boot-test/src/main/java/org/springframework/boot/test/context/SpringBootTestContextBootstrapper.java b/spring-boot-test/src/main/java/org/springframework/boot/test/context/SpringBootTestContextBootstrapper.java index 918ac60ef4c..8a5aea32635 100644 --- a/spring-boot-test/src/main/java/org/springframework/boot/test/context/SpringBootTestContextBootstrapper.java +++ b/spring-boot-test/src/main/java/org/springframework/boot/test/context/SpringBootTestContextBootstrapper.java @@ -56,15 +56,8 @@ public class SpringBootTestContextBootstrapper extends DefaultTestContextBootstr Class[] classes = getOrFindConfigurationClasses(mergedConfig); List 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 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()); + } + } diff --git a/spring-boot-test/src/test/java/org/springframework/boot/test/context/SpringApplicationConfigurationMockMvcTests.java b/spring-boot-test/src/test/java/org/springframework/boot/test/context/SpringApplicationContextLoaderMockMvcTests.java similarity index 92% rename from spring-boot-test/src/test/java/org/springframework/boot/test/context/SpringApplicationConfigurationMockMvcTests.java rename to spring-boot-test/src/test/java/org/springframework/boot/test/context/SpringApplicationContextLoaderMockMvcTests.java index 280163a71cd..93c49d256f6 100644 --- a/spring-boot-test/src/test/java/org/springframework/boot/test/context/SpringApplicationConfigurationMockMvcTests.java +++ b/spring-boot-test/src/test/java/org/springframework/boot/test/context/SpringApplicationContextLoaderMockMvcTests.java @@ -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; diff --git a/spring-boot-test/src/test/java/org/springframework/boot/test/context/SpringApplicationConfigurationActiveProfileTests.java b/spring-boot-test/src/test/java/org/springframework/boot/test/context/SpringApplicationTestActiveProfileTests.java similarity index 89% rename from spring-boot-test/src/test/java/org/springframework/boot/test/context/SpringApplicationConfigurationActiveProfileTests.java rename to spring-boot-test/src/test/java/org/springframework/boot/test/context/SpringApplicationTestActiveProfileTests.java index b16b7167a44..b4ab3b34710 100644 --- a/spring-boot-test/src/test/java/org/springframework/boot/test/context/SpringApplicationConfigurationActiveProfileTests.java +++ b/spring-boot-test/src/test/java/org/springframework/boot/test/context/SpringApplicationTestActiveProfileTests.java @@ -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; diff --git a/spring-boot-test/src/test/java/org/springframework/boot/test/context/SpringApplicationConfigurationDefaultConfigurationTests.java b/spring-boot-test/src/test/java/org/springframework/boot/test/context/SpringApplicationTestDefaultConfigurationTests.java similarity index 87% rename from spring-boot-test/src/test/java/org/springframework/boot/test/context/SpringApplicationConfigurationDefaultConfigurationTests.java rename to spring-boot-test/src/test/java/org/springframework/boot/test/context/SpringApplicationTestDefaultConfigurationTests.java index f8549f85129..784a8b85a1b 100644 --- a/spring-boot-test/src/test/java/org/springframework/boot/test/context/SpringApplicationConfigurationDefaultConfigurationTests.java +++ b/spring-boot-test/src/test/java/org/springframework/boot/test/context/SpringApplicationTestDefaultConfigurationTests.java @@ -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; diff --git a/spring-boot-test/src/test/java/org/springframework/boot/test/context/SpringApplicationConfigurationGroovyConfigurationTests.java b/spring-boot-test/src/test/java/org/springframework/boot/test/context/SpringApplicationTestGroovyConfigurationTests.java similarity index 80% rename from spring-boot-test/src/test/java/org/springframework/boot/test/context/SpringApplicationConfigurationGroovyConfigurationTests.java rename to spring-boot-test/src/test/java/org/springframework/boot/test/context/SpringApplicationTestGroovyConfigurationTests.java index a5bb3c2e7b7..c73edd9de22 100644 --- a/spring-boot-test/src/test/java/org/springframework/boot/test/context/SpringApplicationConfigurationGroovyConfigurationTests.java +++ b/spring-boot-test/src/test/java/org/springframework/boot/test/context/SpringApplicationTestGroovyConfigurationTests.java @@ -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; diff --git a/spring-boot-test/src/test/java/org/springframework/boot/test/context/SpringApplicationConfigurationGroovyConventionConfigurationTests.java b/spring-boot-test/src/test/java/org/springframework/boot/test/context/SpringApplicationTestGroovyConventionConfigurationTests.java similarity index 86% rename from spring-boot-test/src/test/java/org/springframework/boot/test/context/SpringApplicationConfigurationGroovyConventionConfigurationTests.java rename to spring-boot-test/src/test/java/org/springframework/boot/test/context/SpringApplicationTestGroovyConventionConfigurationTests.java index 1df1905e561..13ddc3f6c60 100644 --- a/spring-boot-test/src/test/java/org/springframework/boot/test/context/SpringApplicationConfigurationGroovyConventionConfigurationTests.java +++ b/spring-boot-test/src/test/java/org/springframework/boot/test/context/SpringApplicationTestGroovyConventionConfigurationTests.java @@ -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; diff --git a/spring-boot-test/src/test/java/org/springframework/boot/test/context/SpringApplicationConfigurationJmxTests.java b/spring-boot-test/src/test/java/org/springframework/boot/test/context/SpringApplicationTestJmxTests.java similarity index 89% rename from spring-boot-test/src/test/java/org/springframework/boot/test/context/SpringApplicationConfigurationJmxTests.java rename to spring-boot-test/src/test/java/org/springframework/boot/test/context/SpringApplicationTestJmxTests.java index 6a1a8607b12..4ec8accf325 100644 --- a/spring-boot-test/src/test/java/org/springframework/boot/test/context/SpringApplicationConfigurationJmxTests.java +++ b/spring-boot-test/src/test/java/org/springframework/boot/test/context/SpringApplicationTestJmxTests.java @@ -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; diff --git a/spring-boot-test/src/test/java/org/springframework/boot/test/context/SpringApplicationConfigurationMixedConfigurationTests.java b/spring-boot-test/src/test/java/org/springframework/boot/test/context/SpringApplicationTestMixedConfigurationTests.java similarity index 78% rename from spring-boot-test/src/test/java/org/springframework/boot/test/context/SpringApplicationConfigurationMixedConfigurationTests.java rename to spring-boot-test/src/test/java/org/springframework/boot/test/context/SpringApplicationTestMixedConfigurationTests.java index c4c3597ae34..5ca7d3e05a9 100644 --- a/spring-boot-test/src/test/java/org/springframework/boot/test/context/SpringApplicationConfigurationMixedConfigurationTests.java +++ b/spring-boot-test/src/test/java/org/springframework/boot/test/context/SpringApplicationTestMixedConfigurationTests.java @@ -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; diff --git a/spring-boot-test/src/test/java/org/springframework/boot/test/context/SpringApplicationTestPropertyLocationTests.java b/spring-boot-test/src/test/java/org/springframework/boot/test/context/SpringApplicationTestPropertyLocationTests.java index 7bc67d1a1ff..564a7cbc14a 100644 --- a/spring-boot-test/src/test/java/org/springframework/boot/test/context/SpringApplicationTestPropertyLocationTests.java +++ b/spring-boot-test/src/test/java/org/springframework/boot/test/context/SpringApplicationTestPropertyLocationTests.java @@ -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 { diff --git a/spring-boot-test/src/test/java/org/springframework/boot/test/context/SpringApplicationTestWithClassesIntegrationTests.java b/spring-boot-test/src/test/java/org/springframework/boot/test/context/SpringApplicationTestWithClassesIntegrationTests.java new file mode 100644 index 00000000000..4db2e747092 --- /dev/null +++ b/spring-boot-test/src/test/java/org/springframework/boot/test/context/SpringApplicationTestWithClassesIntegrationTests.java @@ -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 { + + } + +} diff --git a/spring-boot-test/src/test/java/org/springframework/boot/test/context/SpringApplicationTestWithContextConfigurationIntegrationTests.java b/spring-boot-test/src/test/java/org/springframework/boot/test/context/SpringApplicationTestWithContextConfigurationIntegrationTests.java new file mode 100644 index 00000000000..0e3e8d3eb03 --- /dev/null +++ b/spring-boot-test/src/test/java/org/springframework/boot/test/context/SpringApplicationTestWithContextConfigurationIntegrationTests.java @@ -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 { + + } + +} diff --git a/spring-boot-test/src/test/java/org/springframework/boot/test/context/SpringApplicationConfigurationXmlConventionConfigurationTests.java b/spring-boot-test/src/test/java/org/springframework/boot/test/context/SpringApplicationTestXmlConventionConfigurationTests.java similarity index 86% rename from spring-boot-test/src/test/java/org/springframework/boot/test/context/SpringApplicationConfigurationXmlConventionConfigurationTests.java rename to spring-boot-test/src/test/java/org/springframework/boot/test/context/SpringApplicationTestXmlConventionConfigurationTests.java index 2334f366cd4..29834ed4828 100644 --- a/spring-boot-test/src/test/java/org/springframework/boot/test/context/SpringApplicationConfigurationXmlConventionConfigurationTests.java +++ b/spring-boot-test/src/test/java/org/springframework/boot/test/context/SpringApplicationTestXmlConventionConfigurationTests.java @@ -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; diff --git a/spring-boot-test/src/test/java/org/springframework/boot/test/mock/web/SpringBootMockServletContextTests.java b/spring-boot-test/src/test/java/org/springframework/boot/test/mock/web/SpringBootMockServletContextTests.java index 38cfca8149f..a360abbed36 100644 --- a/spring-boot-test/src/test/java/org/springframework/boot/test/mock/web/SpringBootMockServletContextTests.java +++ b/spring-boot-test/src/test/java/org/springframework/boot/test/mock/web/SpringBootMockServletContextTests.java @@ -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 { diff --git a/spring-boot-test/src/test/resources/org/springframework/boot/test/context/SpringApplicationConfigurationGroovyConventionConfigurationTestsContext.groovy b/spring-boot-test/src/test/resources/org/springframework/boot/test/context/SpringApplicationTestGroovyConventionConfigurationTestsContext.groovy similarity index 100% rename from spring-boot-test/src/test/resources/org/springframework/boot/test/context/SpringApplicationConfigurationGroovyConventionConfigurationTestsContext.groovy rename to spring-boot-test/src/test/resources/org/springframework/boot/test/context/SpringApplicationTestGroovyConventionConfigurationTestsContext.groovy diff --git a/spring-boot-test/src/test/resources/org/springframework/boot/test/context/SpringApplicationConfigurationXmlConventionConfigurationTests-context.xml b/spring-boot-test/src/test/resources/org/springframework/boot/test/context/SpringApplicationTestXmlConventionConfigurationTests-context.xml similarity index 100% rename from spring-boot-test/src/test/resources/org/springframework/boot/test/context/SpringApplicationConfigurationXmlConventionConfigurationTests-context.xml rename to spring-boot-test/src/test/resources/org/springframework/boot/test/context/SpringApplicationTestXmlConventionConfigurationTests-context.xml