@@ -6100,37 +6100,38 @@ reference documentation.
6100
6100
A Spring Boot application is a Spring `ApplicationContext`, so nothing very special has
6101
6101
to be done to test it beyond what you would normally do with a vanilla Spring context.
6102
6102
6103
-
6104
6103
NOTE: External properties, logging, and other features of Spring Boot are installed in the
6105
6104
context by default only if you use `SpringApplication` to create it.
6106
6105
6107
6106
Spring Boot provides a `@SpringBootTest` annotation, which can be used as an alternative
6108
6107
to the standard `spring-test` `@ContextConfiguration` annotation when you need Spring
6109
- Boot features. The annotation works by creating the `ApplicationContext` used in your
6110
- tests through `SpringApplication`. In addition to `@SpringBootTest` a number of other
6111
- annotations are also provided for
6108
+ Boot features. The annotation works by
6109
+ <<boot-features-testing-spring-boot-applications-detecting-config,creating the
6110
+ `ApplicationContext` used in your tests through `SpringApplication`>>. In addition to
6111
+ `@SpringBootTest` a number of other annotations are also provided for
6112
6112
<<boot-features-testing-spring-boot-applications-testing-autoconfigured-tests,testing more
6113
6113
specific slices>> of an application.
6114
6114
6115
6115
TIP: Don't forget to also add `@RunWith(SpringRunner.class)` to your test, otherwise
6116
6116
the annotations will be ignored.
6117
6117
6118
- You can use the `webEnvironment` attribute of `@SpringBootTest` to further refine how
6119
- your tests run:
6120
-
6121
- * `MOCK`: Loads a `WebApplicationContext` and provides a mock servlet environment.
6122
- Embedded servlet containers are not started when using this annotation. If servlet APIs
6123
- are not on your classpath, this mode transparently falls back to creating a regular
6124
- non-web `ApplicationContext`. It can be used in conjunction with
6125
- `@AutoConfigureMockMvc` for `MockMvc`-based testing of your application.
6126
- * `RANDOM_PORT`: Loads an `ServletWebServerApplicationContext` and provides a real
6127
- servlet environment. Embedded servlet containers are started and listen on a random
6128
- port.
6129
- * `DEFINED_PORT`: Loads a `ServletWebServerApplicationContext` and provides a real
6130
- servlet environment. Embedded servlet containers are started and listen on a defined port
6131
- (from your `application.properties` or on the default port of `8080`).
6118
+ By default, `@SpringBootTest` will not start a server. You can use the `webEnvironment`
6119
+ attribute of `@SpringBootTest` to further refine how your tests run:
6120
+
6121
+ * `MOCK`(Default) : Loads a web `ApplicationContext` and provides a mock web
6122
+ environment. Embedded servers are not started when using this annotation. If a web
6123
+ environment is not available on your classpath, this mode transparently falls back to
6124
+ creating a regular non-web `ApplicationContext`. It can be used in conjunction with
6125
+ <<boot-features-testing-spring-boot-applications-testing-with-mock-environment,
6126
+ `@AutoConfigureMockMvc` or `@AutoConfigureWebTestClient`>> for mock-based testing of your
6127
+ web application.
6128
+ * `RANDOM_PORT`: Loads a `WebServerApplicationContext` and provides a real web
6129
+ environment. Embedded servers are started and listen on a random port.
6130
+ * `DEFINED_PORT`: Loads a `WebServerApplicationContext` and provides a real web
6131
+ environment. Embedded servers are started and listen on a defined port (from your
6132
+ `application.properties` or on the default port of `8080`).
6132
6133
* `NONE`: Loads an `ApplicationContext` by using `SpringApplication` but does not provide
6133
- _any_ servlet environment (mock or otherwise).
6134
+ _any_ web environment (mock or otherwise).
6134
6135
6135
6136
NOTE: If your test is `@Transactional`, it rolls back the transaction at the end of each
6136
6137
test method by default. However, as using this arrangement with either `RANDOM_PORT` or
@@ -6233,6 +6234,32 @@ NOTE: If you directly use `@ComponentScan` (that is, not through
6233
6234
{dc-spring-boot}/context/TypeExcludeFilter.{dc-ext}[the Javadoc] for details.
6234
6235
6235
6236
6237
+ [[boot-features-testing-spring-boot-applications-testing-with-mock-environment]]
6238
+ ==== Testing with a mock environment
6239
+ By default, `@SpringBootTest` does not start the server. If you have web endpoints that
6240
+ you want to test against this mock environment, you can additionally configure
6241
+ {spring-reference}/testing.html#spring-mvc-test-framework[`MockMvc`] as shown in the
6242
+ following example:
6243
+
6244
+ [source,java,indent=0]
6245
+ ----
6246
+ include::{code-examples}/test/web/MockMvcExampleTests.java[tag=test-mock-mvc]
6247
+ ----
6248
+
6249
+ TIP: If you want to focus only on the web layer and not start a complete
6250
+ `ApplicationContext`, consider
6251
+ <<boot-features-testing-spring-boot-applications-testing-autoconfigured-mvc-tests,using
6252
+ `@WebMvcTest` instead>>.
6253
+
6254
+ Alternatively, you can configure a
6255
+ {spring-reference}testing.html#webtestclient-tests[`WebTestClient`] as shown in the
6256
+ following example:
6257
+
6258
+ [source,java,indent=0]
6259
+ ----
6260
+ include::{code-examples}/test/web/MockWebTestClientExampleTests.java[tag=test-mock-web-test-client]
6261
+ ----
6262
+
6236
6263
6237
6264
6238
6265
[[boot-features-testing-spring-boot-applications-testing-with-running-server]]
0 commit comments