@ -543,15 +543,16 @@ that and be sure that it has initialized is to add a `@Bean` of type
@@ -543,15 +543,16 @@ that and be sure that it has initialized is to add a `@Bean` of type
`ApplicationListener<EmbeddedServletContainerInitializedEvent>` and pull the container
out of the event when it is published.
A useful practice for use with `@WebIntegrationTest` is to set `server.port=0`
and then inject the actual port. For example:
Tests that use `@SpringApplicationTest(webEnvironment=WebEnvironment.RANDOM_PORT)` can
also inject the actual port into a field using the `@LocalServerPort` annotation. For
@ -4349,25 +4349,29 @@ One thing to watch out for though is that the external properties, logging and o
@@ -4349,25 +4349,29 @@ One thing to watch out for though is that the external properties, logging and o
features of Spring Boot are only installed in the context by default if you use
`SpringApplication` to create it.
Spring Boot provides three annotations which can be used as an alternative the standard
`spring-test` `@ContextConfiguration` annotation when you need Spring Boot features. All
three work by creating the `ApplicationContext` used in your tests via
`SpringApplication`.
The specific annotation that you choose will depend on the type of test that you are writing:
* `@SpringApplicationTest` -- Loads an `ApplicationContext` or `WebApplicationContext`
(depending on your classpath) using `SpringApplication` and provides a mock servlet environment. Embedded servlet containers are not started
when using this annotation.
* `@WebIntegrationTest` -- Loads an `EmbeddedWebApplicationContext` using
`SpringApplication` and provides a real servlet environment. Embedded servlet containers
are started and listening on a defined or random port.
* `@IntegrationTest` -- Loads an `ApplicationContext` using `SpringApplication` but does
not provides _any_ servlet environment (mock or otherwise).
NOTE: In addition to `@SpringApplicationTest`, `@WebIntegrationTest` and
`@IntegrationTest` a number of other annotations are also provided for testing more
specific slices of an application. See below for details.
Spring Boot provides a `@SpringApplicationTest` annotation which can be used as an
alternative the standard `spring-test` `@ContextConfiguration` annotation when you need
Spring Boot features. The annotation works by creating the `ApplicationContext` used
in your tests via `SpringApplication`.
You can use the `webEnvironment` attribute of `@SpringApplicationTest` to further refine
how your tests will run:
* `MOCK` -- Loads a `WebApplicationContext` and provides a mock servlet environment.
Embedded servlet containers are not started when using this annotation. If servlet
APIs are not on your classpath this mode will transparantly fallback to creating a
regular non-web `ApplicationContext`.
* `RANDOM_PORT` -- Loads an `EmbeddedWebApplicationContext` and provides a real
servlet environment. Embedded servlet containers are started and listening on a random
port.
* `DEFINED_PORT` -- Loads an `EmbeddedWebApplicationContext` and provides a real
servlet environment. Embedded servlet containers are started and listening on a defined
port (i.e from your `application.properties` or on the default port `8080`).
* `NONE` -- Loads an `ApplicationContext` using `SpringApplication` but does not provides
_any_ servlet environment (mock or otherwise).
NOTE: In addition to `@SpringApplicationTest` a number of other annotations are also
provided for testing more specific slices of an application. See below for details.
TIP: Don't forget to also add `@RunWith(SpringRunner.class)` to your test, otherwise
the annotations will be ignored.
@ -4416,62 +4420,18 @@ will need to register the `TypeExcludeFilter` with it. See
@@ -4416,62 +4420,18 @@ will need to register the `TypeExcludeFilter` with it. See