Rework SpringApplicationTest to support web modes

Rework the new testing support so that @SpringApplicationTest can be
used for standard integration tests, web integration tests with a
mock Servlet environment and web integration tests with an embedded
servlet container. This means that it a replacement for 1.3's
@IntegrationTest and @WebIntegrationTest and allows all
SpringApplication testing to be configured using a common annotation.

The old @IntegrationTest and @WebIntegrationTest along with their
supporting classes have been reinstated to their previous form (while
remaining deprecated). This should ensure that they continue to work
in 1.4 exactly as they did in 1.3 giving users a smooth path to
@SpringApplicationTest.

See gh-5477
This commit is contained in:
Andy Wilkinson
2016-03-31 16:55:28 +01:00
committed by Phillip Webb
parent 893a6c32f3
commit 33f0ea3480
122 changed files with 947 additions and 823 deletions

View File

@@ -24,7 +24,8 @@ import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.security.SecurityProperties;
import org.springframework.boot.context.web.LocalServerPort;
import org.springframework.boot.test.context.web.WebIntegrationTest;
import org.springframework.boot.test.context.SpringApplicationTest;
import org.springframework.boot.test.context.SpringApplicationTest.WebEnvironment;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
@@ -40,7 +41,7 @@ import static org.assertj.core.api.Assertions.assertThat;
* @author Dave Syer
*/
@RunWith(SpringRunner.class)
@WebIntegrationTest(randomPort = true)
@SpringApplicationTest(webEnvironment = WebEnvironment.RANDOM_PORT)
@DirtiesContext
@ActiveProfiles("endpoints")
public class EndpointsPropertiesSampleActuatorApplicationTests {

View File

@@ -25,7 +25,8 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.security.SecurityProperties;
import org.springframework.boot.context.web.LocalServerPort;
import org.springframework.boot.test.context.web.WebIntegrationTest;
import org.springframework.boot.test.context.SpringApplicationTest;
import org.springframework.boot.test.context.SpringApplicationTest.WebEnvironment;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
@@ -40,8 +41,9 @@ import static org.assertj.core.api.Assertions.assertThat;
* @author Dave Syer
*/
@RunWith(SpringRunner.class)
@WebIntegrationTest(value = { "management.port=0", "management.context-path=/admin",
"management.security.enabled=false" }, randomPort = true)
@SpringApplicationTest(webEnvironment = WebEnvironment.RANDOM_PORT, properties = {
"management.port=0", "management.context-path=/admin",
"management.security.enabled=false" })
@DirtiesContext
public class InsecureManagementPortAndPathSampleActuatorApplicationTests {

View File

@@ -22,7 +22,8 @@ import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.context.web.LocalServerPort;
import org.springframework.boot.test.context.web.WebIntegrationTest;
import org.springframework.boot.test.context.SpringApplicationTest;
import org.springframework.boot.test.context.SpringApplicationTest.WebEnvironment;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
@@ -39,7 +40,8 @@ import static org.assertj.core.api.Assertions.assertThat;
* @author Dave Syer
*/
@RunWith(SpringRunner.class)
@WebIntegrationTest(value = { "management.security.enabled:false" }, randomPort = true)
@SpringApplicationTest(webEnvironment = WebEnvironment.RANDOM_PORT, properties = {
"management.security.enabled:false" })
@DirtiesContext
@ActiveProfiles("unsecure-management")
public class InsecureManagementSampleActuatorApplicationTests {

View File

@@ -22,7 +22,8 @@ import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.context.web.LocalServerPort;
import org.springframework.boot.test.context.web.WebIntegrationTest;
import org.springframework.boot.test.context.SpringApplicationTest;
import org.springframework.boot.test.context.SpringApplicationTest.WebEnvironment;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
@@ -38,7 +39,8 @@ import static org.assertj.core.api.Assertions.assertThat;
* @author Dave Syer
*/
@RunWith(SpringRunner.class)
@WebIntegrationTest(value = { "security.basic.enabled:false" }, randomPort = true)
@SpringApplicationTest(webEnvironment = WebEnvironment.RANDOM_PORT, properties = {
"security.basic.enabled:false" })
@DirtiesContext
public class InsecureSampleActuatorApplicationTests {

View File

@@ -23,7 +23,8 @@ import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.web.LocalServerPort;
import org.springframework.boot.test.context.web.WebIntegrationTest;
import org.springframework.boot.test.context.SpringApplicationTest;
import org.springframework.boot.test.context.SpringApplicationTest.WebEnvironment;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
@@ -38,8 +39,9 @@ import static org.assertj.core.api.Assertions.assertThat;
* @author Dave Syer
*/
@RunWith(SpringRunner.class)
@WebIntegrationTest(value = { "management.port=0", "management.address=127.0.0.1",
"management.context-path:/admin" }, randomPort = true)
@SpringApplicationTest(webEnvironment = WebEnvironment.RANDOM_PORT, properties = {
"management.port=0", "management.address=127.0.0.1",
"management.context-path:/admin" })
@DirtiesContext
public class ManagementAddressActuatorApplicationTests {

View File

@@ -22,7 +22,8 @@ import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.context.web.LocalServerPort;
import org.springframework.boot.test.context.web.WebIntegrationTest;
import org.springframework.boot.test.context.SpringApplicationTest;
import org.springframework.boot.test.context.SpringApplicationTest.WebEnvironment;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
@@ -37,7 +38,8 @@ import static org.assertj.core.api.Assertions.assertThat;
* @author Dave Syer
*/
@RunWith(SpringRunner.class)
@WebIntegrationTest(value = { "management.context_path=/admin" }, randomPort = true)
@SpringApplicationTest(webEnvironment = WebEnvironment.RANDOM_PORT, properties = {
"management.context_path=/admin" })
@DirtiesContext
public class ManagementPathSampleActuatorApplicationTests {

View File

@@ -25,7 +25,8 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.security.SecurityProperties;
import org.springframework.boot.context.web.LocalServerPort;
import org.springframework.boot.test.context.web.WebIntegrationTest;
import org.springframework.boot.test.context.SpringApplicationTest;
import org.springframework.boot.test.context.SpringApplicationTest.WebEnvironment;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
@@ -40,8 +41,8 @@ import static org.assertj.core.api.Assertions.assertThat;
* @author Dave Syer
*/
@RunWith(SpringRunner.class)
@WebIntegrationTest(value = { "management.port=0",
"management.context-path=/admin" }, randomPort = true)
@SpringApplicationTest(webEnvironment = WebEnvironment.RANDOM_PORT, properties = {
"management.port=0", "management.context-path=/admin" })
@DirtiesContext
public class ManagementPortAndPathSampleActuatorApplicationTests {

View File

@@ -25,7 +25,8 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.security.SecurityProperties;
import org.springframework.boot.context.web.LocalServerPort;
import org.springframework.boot.test.context.web.WebIntegrationTest;
import org.springframework.boot.test.context.SpringApplicationTest;
import org.springframework.boot.test.context.SpringApplicationTest.WebEnvironment;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
@@ -40,7 +41,8 @@ import static org.assertj.core.api.Assertions.assertThat;
* @author Dave Syer
*/
@RunWith(SpringRunner.class)
@WebIntegrationTest(value = { "management.port=0" }, randomPort = true)
@SpringApplicationTest(webEnvironment = WebEnvironment.RANDOM_PORT, properties = {
"management.port=0" })
@DirtiesContext
public class ManagementPortSampleActuatorApplicationTests {

View File

@@ -24,7 +24,8 @@ import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.security.SecurityProperties;
import org.springframework.boot.context.web.LocalServerPort;
import org.springframework.boot.test.context.web.WebIntegrationTest;
import org.springframework.boot.test.context.SpringApplicationTest;
import org.springframework.boot.test.context.SpringApplicationTest.WebEnvironment;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
@@ -39,7 +40,8 @@ import static org.assertj.core.api.Assertions.assertThat;
* @author Dave Syer
*/
@RunWith(SpringRunner.class)
@WebIntegrationTest(value = { "management.port=-1" }, randomPort = true)
@SpringApplicationTest(webEnvironment = WebEnvironment.RANDOM_PORT, properties = {
"management.port=-1" })
@DirtiesContext
public class NoManagementSampleActuatorApplicationTests {

View File

@@ -20,7 +20,8 @@ import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.context.web.LocalServerPort;
import org.springframework.boot.test.context.web.WebIntegrationTest;
import org.springframework.boot.test.context.SpringApplicationTest;
import org.springframework.boot.test.context.SpringApplicationTest.WebEnvironment;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
@@ -35,7 +36,8 @@ import static org.assertj.core.api.Assertions.assertThat;
* @author Phillip Webb
*/
@RunWith(SpringRunner.class)
@WebIntegrationTest(value = { "endpoints.health.sensitive=false" }, randomPort = true)
@SpringApplicationTest(webEnvironment = WebEnvironment.RANDOM_PORT, properties = {
"endpoints.health.sensitive=false" })
@DirtiesContext
public class NonSensitiveHealthTests {

View File

@@ -27,7 +27,8 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
import org.springframework.boot.autoconfigure.security.SecurityProperties;
import org.springframework.boot.context.web.LocalServerPort;
import org.springframework.boot.test.context.web.WebIntegrationTest;
import org.springframework.boot.test.context.SpringApplicationTest;
import org.springframework.boot.test.context.SpringApplicationTest.WebEnvironment;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
@@ -47,7 +48,7 @@ import static org.assertj.core.api.Assertions.assertThat;
* @author Stephane Nicoll
*/
@RunWith(SpringRunner.class)
@WebIntegrationTest(randomPort = true)
@SpringApplicationTest(webEnvironment = WebEnvironment.RANDOM_PORT)
@DirtiesContext
public class SampleActuatorApplicationTests {

View File

@@ -22,7 +22,8 @@ import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.context.web.LocalServerPort;
import org.springframework.boot.test.context.web.WebIntegrationTest;
import org.springframework.boot.test.context.SpringApplicationTest;
import org.springframework.boot.test.context.SpringApplicationTest.WebEnvironment;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
@@ -38,8 +39,8 @@ import static org.assertj.core.api.Assertions.assertThat;
* @author Dave Syer
*/
@RunWith(SpringRunner.class)
@WebIntegrationTest(value = { "security.basic.enabled:false",
"server.servletPath:/spring" }, randomPort = true)
@SpringApplicationTest(webEnvironment = WebEnvironment.RANDOM_PORT, properties = {
"security.basic.enabled:false", "server.servletPath:/spring" })
@DirtiesContext
public class ServletPathInsecureSampleActuatorApplicationTests {

View File

@@ -22,7 +22,8 @@ import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.context.web.LocalServerPort;
import org.springframework.boot.test.context.web.WebIntegrationTest;
import org.springframework.boot.test.context.SpringApplicationTest;
import org.springframework.boot.test.context.SpringApplicationTest.WebEnvironment;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
@@ -37,7 +38,8 @@ import static org.assertj.core.api.Assertions.assertThat;
* @author Dave Syer
*/
@RunWith(SpringRunner.class)
@WebIntegrationTest(value = { "server.servletPath=/spring" }, randomPort = true)
@SpringApplicationTest(webEnvironment = WebEnvironment.RANDOM_PORT, properties = {
"server.servletPath=/spring" })
@DirtiesContext
public class ServletPathSampleActuatorApplicationTests {

View File

@@ -24,7 +24,8 @@ import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.security.SecurityProperties;
import org.springframework.boot.context.web.LocalServerPort;
import org.springframework.boot.test.context.web.WebIntegrationTest;
import org.springframework.boot.test.context.SpringApplicationTest;
import org.springframework.boot.test.context.SpringApplicationTest.WebEnvironment;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
@@ -39,7 +40,7 @@ import static org.assertj.core.api.Assertions.assertThat;
* @author Dave Syer
*/
@RunWith(SpringRunner.class)
@WebIntegrationTest(randomPort = true)
@SpringApplicationTest(webEnvironment = WebEnvironment.RANDOM_PORT)
@DirtiesContext
public class ShutdownSampleActuatorApplicationTests {