diff --git a/spring-boot-samples/spring-boot-sample-tomcat/src/test/java/sample/tomcat/SampleTomcatApplicationTests.java b/spring-boot-samples/spring-boot-sample-tomcat/src/test/java/sample/tomcat/SampleTomcatApplicationTests.java
index 9a77b9d51e..f24ec1536f 100644
--- a/spring-boot-samples/spring-boot-sample-tomcat/src/test/java/sample/tomcat/SampleTomcatApplicationTests.java
+++ b/spring-boot-samples/spring-boot-sample-tomcat/src/test/java/sample/tomcat/SampleTomcatApplicationTests.java
@@ -1,5 +1,5 @@
/*
- * Copyright 2012-2014 the original author or authors.
+ * Copyright 2012-2015 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.
@@ -19,14 +19,13 @@ package sample.tomcat;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Value;
-import org.springframework.boot.test.IntegrationTest;
import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.boot.test.TestRestTemplate;
+import org.springframework.boot.test.WebIntegrationTest;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
-import org.springframework.test.context.web.WebAppConfiguration;
import static org.junit.Assert.assertEquals;
@@ -37,8 +36,7 @@ import static org.junit.Assert.assertEquals;
*/
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = SampleTomcatApplication.class)
-@WebAppConfiguration
-@IntegrationTest("server.port:0")
+@WebIntegrationTest(randomPort = true)
@DirtiesContext
public class SampleTomcatApplicationTests {
diff --git a/spring-boot/src/main/java/org/springframework/boot/test/IntegrationTest.java b/spring-boot/src/main/java/org/springframework/boot/test/IntegrationTest.java
index f7fc243112..2a2d14c478 100644
--- a/spring-boot/src/main/java/org/springframework/boot/test/IntegrationTest.java
+++ b/spring-boot/src/main/java/org/springframework/boot/test/IntegrationTest.java
@@ -32,8 +32,8 @@ import org.springframework.test.context.transaction.TransactionalTestExecutionLi
/**
* Test class annotation signifying that the tests are "integration tests" and therefore
- * require full startup in the same way as a production application (listening on normal
- * ports). Normally used in conjunction with {@code @SpringApplicationConfiguration}.
+ * require full startup in the same way as a production application. Normally used in
+ * conjunction with {@code @SpringApplicationConfiguration}.
*
* If your test also uses {@code @WebAppConfiguration} consider using the
* {@link WebIntegrationTest} instead.
diff --git a/spring-boot/src/main/java/org/springframework/boot/test/MergedContextConfigurationProperties.java b/spring-boot/src/main/java/org/springframework/boot/test/MergedContextConfigurationProperties.java
index 7494ab1ac4..6a7475e870 100644
--- a/spring-boot/src/main/java/org/springframework/boot/test/MergedContextConfigurationProperties.java
+++ b/spring-boot/src/main/java/org/springframework/boot/test/MergedContextConfigurationProperties.java
@@ -37,10 +37,11 @@ class MergedContextConfigurationProperties {
this.configuration = configuration;
}
- public void add(String[] properties) {
+ public void add(String[] properties, String... additional) {
Set merged = new LinkedHashSet((Arrays.asList(this.configuration
.getPropertySourceProperties())));
merged.addAll(Arrays.asList(properties));
+ merged.addAll(Arrays.asList(additional));
addIntegrationTestProperty(merged);
ReflectionTestUtils.setField(this.configuration, "propertySourceProperties",
merged.toArray(new String[merged.size()]));
diff --git a/spring-boot/src/main/java/org/springframework/boot/test/SpringApplicationContextLoader.java b/spring-boot/src/main/java/org/springframework/boot/test/SpringApplicationContextLoader.java
index c827e5365d..ca6f900f83 100644
--- a/spring-boot/src/main/java/org/springframework/boot/test/SpringApplicationContextLoader.java
+++ b/spring-boot/src/main/java/org/springframework/boot/test/SpringApplicationContextLoader.java
@@ -46,30 +46,28 @@ import org.springframework.test.context.support.AbstractContextLoader;
import org.springframework.test.context.support.AnnotationConfigContextLoaderUtils;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.context.web.WebMergedContextConfiguration;
+import org.springframework.util.Assert;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.context.support.GenericWebApplicationContext;
/**
* A {@link ContextLoader} that can be used to test Spring Boot applications (those that
- * normally startup using {@link SpringApplication}). Normally never starts an embedded
- * web server, but detects the {@link WebAppConfiguration @WebAppConfiguration} annotation
- * on the test class and only creates a web application context if it is present. Non-web
- * features, like a repository layer, can be tested cleanly by simply not marking
- * the test class @WebAppConfiguration.
+ * normally startup using {@link SpringApplication}). Can be used to test non-web features
+ * (like a repository layer) or start an fully-configured embedded servlet container.
*
- * If you want to start a web server, mark the test class as
- * @WebAppConfiguration @IntegrationTest. This is useful for testing HTTP
- * endpoints using {@link TestRestTemplate} (for instance), especially since you can
- * @Autowired application context components into your test case to see the
- * internal effects of HTTP requests directly.
+ * Use {@code @WebIntegrationTest} (or {@code @IntegrationTest} with
+ * {@code @WebAppConfiguration}) to indicate that you want to use a real servlet container
+ * or {@code @WebAppConfiguration} alone to use a {@link MockServletContext}.
*
* If @ActiveProfiles are provided in the test class they will be used to
* create the application context.
*
* @author Dave Syer
+ * @author Phillip Webb
* @see IntegrationTest
* @see WebIntegrationTest
+ * @see TestRestTemplate
*/
public class SpringApplicationContextLoader extends AbstractContextLoader {
@@ -78,21 +76,15 @@ public class SpringApplicationContextLoader extends AbstractContextLoader {
@Override
public ApplicationContext loadContext(MergedContextConfiguration config)
throws Exception {
+ assertValidAnnotations(config.getTestClass());
SpringApplication application = getSpringApplication();
application.setSources(getSources(config));
ConfigurableEnvironment environment = new StandardEnvironment();
if (!ObjectUtils.isEmpty(config.getActiveProfiles())) {
- String profiles = StringUtils.arrayToCommaDelimitedString(config
- .getActiveProfiles());
- EnvironmentTestUtils.addEnvironment(environment, "spring.profiles.active="
- + profiles);
+ setActiveProfiles(environment, config.getActiveProfiles());
}
- // Ensure @IntegrationTest properties go before external config and after system
- environment.getPropertySources()
- .addAfter(
- StandardEnvironment.SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME,
- new MapPropertySource("integrationTest",
- getEnvironmentProperties(config)));
+ Map properties = getEnvironmentProperties(config);
+ addProperties(environment, properties);
application.setEnvironment(environment);
List> initializers = getInitializers(config,
application);
@@ -106,13 +98,14 @@ public class SpringApplicationContextLoader extends AbstractContextLoader {
return application.run();
}
- @Override
- public void processContextConfiguration(
- ContextConfigurationAttributes configAttributes) {
- if (!configAttributes.hasLocations() && !configAttributes.hasClasses()) {
- Class>[] defaultConfigClasses = detectDefaultConfigurationClasses(configAttributes
- .getDeclaringClass());
- configAttributes.setClasses(defaultConfigClasses);
+ private void assertValidAnnotations(Class> testClass) {
+ boolean hasWebAppConfiguration = AnnotationUtils.findAnnotation(testClass,
+ WebAppConfiguration.class) != null;
+ boolean hasWebIntegrationTest = AnnotationUtils.findAnnotation(testClass,
+ WebIntegrationTest.class) != null;
+ if (hasWebAppConfiguration && hasWebIntegrationTest) {
+ throw new IllegalStateException("@WebIntegrationTest and "
+ + "@WebAppConfiguration cannot be used together");
}
}
@@ -129,27 +122,16 @@ public class SpringApplicationContextLoader extends AbstractContextLoader {
Set