Skip to content

Commit a1ef3f0

Browse files
committed
Improve documentation for using a mock environment with @SpringBootTest
Closes gh-13827
1 parent ebffe6e commit a1ef3f0

File tree

3 files changed

+140
-19
lines changed

3 files changed

+140
-19
lines changed

spring-boot-project/spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc

Lines changed: 46 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6100,37 +6100,38 @@ reference documentation.
61006100
A Spring Boot application is a Spring `ApplicationContext`, so nothing very special has
61016101
to be done to test it beyond what you would normally do with a vanilla Spring context.
61026102

6103-
61046103
NOTE: External properties, logging, and other features of Spring Boot are installed in the
61056104
context by default only if you use `SpringApplication` to create it.
61066105

61076106
Spring Boot provides a `@SpringBootTest` annotation, which can be used as an alternative
61086107
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
61126112
<<boot-features-testing-spring-boot-applications-testing-autoconfigured-tests,testing more
61136113
specific slices>> of an application.
61146114

61156115
TIP: Don't forget to also add `@RunWith(SpringRunner.class)` to your test, otherwise
61166116
the annotations will be ignored.
61176117

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`).
61326133
* `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).
61346135

61356136
NOTE: If your test is `@Transactional`, it rolls back the transaction at the end of each
61366137
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
62336234
{dc-spring-boot}/context/TypeExcludeFilter.{dc-ext}[the Javadoc] for details.
62346235

62356236

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+
62366263

62376264

62386265
[[boot-features-testing-spring-boot-applications-testing-with-running-server]]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* Copyright 2012-2018 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.docs.test.web;
18+
19+
// tag::test-mock-mvc[]
20+
21+
import org.junit.Test;
22+
import org.junit.runner.RunWith;
23+
24+
import org.springframework.beans.factory.annotation.Autowired;
25+
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
26+
import org.springframework.boot.test.context.SpringBootTest;
27+
import org.springframework.test.context.junit4.SpringRunner;
28+
import org.springframework.test.web.servlet.MockMvc;
29+
30+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
31+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
32+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
33+
34+
@RunWith(SpringRunner.class)
35+
@SpringBootTest
36+
@AutoConfigureMockMvc
37+
public class MockMvcExampleTests {
38+
39+
@Autowired
40+
private MockMvc mvc;
41+
42+
@Test
43+
public void exampleTest() throws Exception {
44+
this.mvc.perform(get("/")).andExpect(status().isOk())
45+
.andExpect(content().string("Hello World"));
46+
}
47+
48+
}
49+
// end::test-mock-mvc[]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* Copyright 2012-2018 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.docs.test.web;
18+
19+
// tag::test-mock-web-test-client[]
20+
21+
import org.junit.Test;
22+
import org.junit.runner.RunWith;
23+
24+
import org.springframework.beans.factory.annotation.Autowired;
25+
import org.springframework.boot.test.autoconfigure.web.reactive.AutoConfigureWebTestClient;
26+
import org.springframework.boot.test.context.SpringBootTest;
27+
import org.springframework.test.context.junit4.SpringRunner;
28+
import org.springframework.test.web.reactive.server.WebTestClient;
29+
30+
@RunWith(SpringRunner.class)
31+
@SpringBootTest
32+
@AutoConfigureWebTestClient
33+
public class MockWebTestClientExampleTests {
34+
35+
@Autowired
36+
private WebTestClient webClient;
37+
38+
@Test
39+
public void exampleTest() {
40+
this.webClient.get().uri("/").exchange().expectStatus().isOk()
41+
.expectBody(String.class).isEqualTo("Hello World");
42+
}
43+
44+
}
45+
// end::test-mock-web-test-client[]

0 commit comments

Comments
 (0)