diff --git a/docs/howto.md b/docs/howto.md index 67fc14b125..86a1c71f23 100644 --- a/docs/howto.md +++ b/docs/howto.md @@ -241,13 +241,12 @@ normally do with a vanilla Spring context. One thing to watch out for though is that the external properties, logging and other features of Spring Boot are only installed in the context by default if you use `SpringApplication` to create it. Spring Boot has a special Spring -`TestContextLoader` which makes this job easy. For example (from the -JPA Sample): +`@ContextConfiguration` annotation, so you can use this for example +(from the JPA Sample): ```java @RunWith(SpringJUnit4ClassRunner.class) -@ContextConfiguration(classes = SampleDataJpaApplication.class, - loader = SpringApplicationContextLoader.class) +@SpringApplicationConfiguration(classes = SampleDataJpaApplication.class) public class CityRepositoryIntegrationTests { @Autowired @@ -256,12 +255,12 @@ public class CityRepositoryIntegrationTests { ... ``` -To use the `SpringApplicationContextLoader` you need the test jar on +To use the `@SpringApplicationConfiguration` you need the test jar on your classpath (recommended Maven co-ordinates "org.springframework.boot:spring-boot-starter-test"). The context loader guesses whether you want to test a web application or not (e.g. with `MockMVC`) by looking for the `@WebAppConfiguration` -annotation (`MockMVC` and `@WebAppConfiguration` are from the Spring +annotation. (`MockMVC` and `@WebAppConfiguration` are from the Spring Test support library). diff --git a/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/web/BasicErrorControllerIntegrationTests.java b/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/web/BasicErrorControllerIntegrationTests.java index 94dd58af7e..7dbfefdf89 100644 --- a/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/web/BasicErrorControllerIntegrationTests.java +++ b/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/web/BasicErrorControllerIntegrationTests.java @@ -28,11 +28,10 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.SpringApplication; import org.springframework.boot.actuate.web.BasicErrorControllerIntegrationTests.TestConfiguration; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; -import org.springframework.boot.context.initializer.LoggingApplicationContextInitializer; +import org.springframework.boot.test.SpringApplicationConfiguration; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.http.MediaType; -import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.context.web.WebAppConfiguration; import org.springframework.test.web.servlet.MockMvc; @@ -49,7 +48,7 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers. /** * @author Dave Syer */ -@ContextConfiguration(classes = TestConfiguration.class, initializers = { LoggingApplicationContextInitializer.class }) +@SpringApplicationConfiguration(classes = TestConfiguration.class) @RunWith(SpringJUnit4ClassRunner.class) @WebAppConfiguration public class BasicErrorControllerIntegrationTests { diff --git a/spring-boot-samples/spring-boot-sample-data-jpa/src/test/java/org/springframework/boot/sample/data/jpa/SampleDataJpaApplicationTests.java b/spring-boot-samples/spring-boot-sample-data-jpa/src/test/java/org/springframework/boot/sample/data/jpa/SampleDataJpaApplicationTests.java index 6560b19a21..ce176c4054 100644 --- a/spring-boot-samples/spring-boot-sample-data-jpa/src/test/java/org/springframework/boot/sample/data/jpa/SampleDataJpaApplicationTests.java +++ b/spring-boot-samples/spring-boot-sample-data-jpa/src/test/java/org/springframework/boot/sample/data/jpa/SampleDataJpaApplicationTests.java @@ -8,9 +8,8 @@ import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.test.SpringApplicationContextLoader; +import org.springframework.boot.test.SpringApplicationConfiguration; import org.springframework.test.context.ActiveProfiles; -import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.context.web.WebAppConfiguration; import org.springframework.test.web.servlet.MockMvc; @@ -23,7 +22,7 @@ import org.springframework.web.context.WebApplicationContext; * @author Oliver Gierke */ @RunWith(SpringJUnit4ClassRunner.class) -@ContextConfiguration(classes = SampleDataJpaApplication.class, loader = SpringApplicationContextLoader.class) +@SpringApplicationConfiguration(classes = SampleDataJpaApplication.class) @WebAppConfiguration @ActiveProfiles("scratch") // Separate profile for web tests to avoid clashing databases diff --git a/spring-boot-samples/spring-boot-sample-data-jpa/src/test/java/org/springframework/boot/sample/data/jpa/service/CityRepositoryIntegrationTests.java b/spring-boot-samples/spring-boot-sample-data-jpa/src/test/java/org/springframework/boot/sample/data/jpa/service/CityRepositoryIntegrationTests.java index 1b263beb1c..d8bb3538ac 100644 --- a/spring-boot-samples/spring-boot-sample-data-jpa/src/test/java/org/springframework/boot/sample/data/jpa/service/CityRepositoryIntegrationTests.java +++ b/spring-boot-samples/spring-boot-sample-data-jpa/src/test/java/org/springframework/boot/sample/data/jpa/service/CityRepositoryIntegrationTests.java @@ -15,8 +15,8 @@ */ package org.springframework.boot.sample.data.jpa.service; -import static org.hamcrest.Matchers.is; import static org.hamcrest.Matchers.greaterThan; +import static org.hamcrest.Matchers.is; import static org.junit.Assert.assertThat; import org.junit.Test; @@ -24,10 +24,9 @@ import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.sample.data.jpa.SampleDataJpaApplication; import org.springframework.boot.sample.data.jpa.domain.City; -import org.springframework.boot.test.SpringApplicationContextLoader; +import org.springframework.boot.test.SpringApplicationConfiguration; import org.springframework.data.domain.Page; import org.springframework.data.domain.PageRequest; -import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; /** @@ -36,7 +35,7 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; * @author Oliver Gierke */ @RunWith(SpringJUnit4ClassRunner.class) -@ContextConfiguration(classes = SampleDataJpaApplication.class, loader = SpringApplicationContextLoader.class) +@SpringApplicationConfiguration(classes = SampleDataJpaApplication.class) public class CityRepositoryIntegrationTests { @Autowired diff --git a/spring-boot-samples/spring-boot-sample-data-jpa/src/test/java/org/springframework/boot/sample/data/jpa/service/HotelRepositoryIntegrationTests.java b/spring-boot-samples/spring-boot-sample-data-jpa/src/test/java/org/springframework/boot/sample/data/jpa/service/HotelRepositoryIntegrationTests.java index bbc86e4811..7c6435c399 100644 --- a/spring-boot-samples/spring-boot-sample-data-jpa/src/test/java/org/springframework/boot/sample/data/jpa/service/HotelRepositoryIntegrationTests.java +++ b/spring-boot-samples/spring-boot-sample-data-jpa/src/test/java/org/springframework/boot/sample/data/jpa/service/HotelRepositoryIntegrationTests.java @@ -15,9 +15,9 @@ */ package org.springframework.boot.sample.data.jpa.service; +import static org.hamcrest.Matchers.greaterThan; import static org.hamcrest.Matchers.hasSize; import static org.hamcrest.Matchers.is; -import static org.hamcrest.Matchers.greaterThan; import static org.junit.Assert.assertThat; import java.util.List; @@ -31,11 +31,10 @@ import org.springframework.boot.sample.data.jpa.domain.Hotel; import org.springframework.boot.sample.data.jpa.domain.HotelSummary; import org.springframework.boot.sample.data.jpa.domain.Rating; import org.springframework.boot.sample.data.jpa.domain.RatingCount; -import org.springframework.boot.test.SpringApplicationContextLoader; +import org.springframework.boot.test.SpringApplicationConfiguration; import org.springframework.data.domain.Page; import org.springframework.data.domain.PageRequest; import org.springframework.data.domain.Sort.Direction; -import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; /** @@ -44,7 +43,7 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; * @author Oliver Gierke */ @RunWith(SpringJUnit4ClassRunner.class) -@ContextConfiguration(classes = SampleDataJpaApplication.class, loader = SpringApplicationContextLoader.class) +@SpringApplicationConfiguration(classes = SampleDataJpaApplication.class) public class HotelRepositoryIntegrationTests { @Autowired diff --git a/spring-boot/src/test/java/org/springframework/boot/test/SpringApplicationConfiguration.java b/spring-boot/src/test/java/org/springframework/boot/test/SpringApplicationConfiguration.java new file mode 100644 index 0000000000..b92c1e5ac7 --- /dev/null +++ b/spring-boot/src/test/java/org/springframework/boot/test/SpringApplicationConfiguration.java @@ -0,0 +1,76 @@ +/* + * Copyright 2012-2013 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; + +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.test.context.ContextConfiguration; + +/** + * @author Dave Syer + */ + +@ContextConfiguration(loader = SpringApplicationContextLoader.class) +@Documented +@Inherited +@Retention(RetentionPolicy.RUNTIME) +@Target(ElementType.TYPE) +public @interface SpringApplicationConfiguration { + + /** + * @see ContextConfiguration#value() + */ + String[] value() default {}; + + /** + * @see ContextConfiguration#locations() + */ + String[] locations() default {}; + + /** + * @see ContextConfiguration#classes() + */ + Class[] classes() default {}; + + /** + * @see ContextConfiguration#initializers() + */ + Class>[] initializers() default {}; + + /** + * @see ContextConfiguration#inheritLocations() + */ + boolean inheritLocations() default true; + + /** + * @see ContextConfiguration#inheritInitializers() + */ + boolean inheritInitializers() default true; + + /** + * @see ContextConfiguration#name() + */ + String name() default ""; + +}