From 3e6c1b435ff6930041a04a4e421c64a115f20bc3 Mon Sep 17 00:00:00 2001 From: Dave Syer Date: Thu, 28 Nov 2013 14:35:36 +0000 Subject: [PATCH] Add @SpringApplicationConfiguration (for integration testing) Example: @RunWith(SpringJUnit4ClassRunner.class) @SpringApplicationConfiguration(classes = SampleDataJpaApplication.class) public class CityRepositoryIntegrationTests { @Autowired CityRepository repository; Fixes gh-66. --- docs/howto.md | 11 ++- .../BasicErrorControllerIntegrationTests.java | 5 +- .../jpa/SampleDataJpaApplicationTests.java | 5 +- .../CityRepositoryIntegrationTests.java | 7 +- .../HotelRepositoryIntegrationTests.java | 7 +- .../test/SpringApplicationConfiguration.java | 76 +++++++++++++++++++ 6 files changed, 91 insertions(+), 20 deletions(-) create mode 100644 spring-boot/src/test/java/org/springframework/boot/test/SpringApplicationConfiguration.java diff --git a/docs/howto.md b/docs/howto.md index 67fc14b1250..86a1c71f236 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 94dd58af7eb..7dbfefdf892 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 6560b19a21f..ce176c4054c 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 1b263beb1cb..d8bb3538ac4 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 bbc86e4811b..7c6435c3999 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 00000000000..b92c1e5ac7d --- /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 ""; + +}