mirror of
https://github.com/spring-projects/spring-boot.git
synced 2024-09-03 04:26:12 +08:00
Add @SpringApplicationConfiguration (for integration testing)
Example: @RunWith(SpringJUnit4ClassRunner.class) @SpringApplicationConfiguration(classes = SampleDataJpaApplication.class) public class CityRepositoryIntegrationTests { @Autowired CityRepository repository; Fixes gh-66.
This commit is contained in:
parent
3e5e058b02
commit
3e6c1b435f
@ -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).
|
||||
|
||||
<span id="main.properties"/>
|
||||
|
@ -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 {
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
@ -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<? extends ApplicationContextInitializer<? extends ConfigurableApplicationContext>>[] initializers() default {};
|
||||
|
||||
/**
|
||||
* @see ContextConfiguration#inheritLocations()
|
||||
*/
|
||||
boolean inheritLocations() default true;
|
||||
|
||||
/**
|
||||
* @see ContextConfiguration#inheritInitializers()
|
||||
*/
|
||||
boolean inheritInitializers() default true;
|
||||
|
||||
/**
|
||||
* @see ContextConfiguration#name()
|
||||
*/
|
||||
String name() default "";
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user