Skip to content

Commit 6518a56

Browse files
committed
Revise introductory content for MockMvc
1 parent 4fb70b6 commit 6518a56

File tree

5 files changed

+42
-39
lines changed

5 files changed

+42
-39
lines changed

framework-docs/modules/ROOT/pages/testing/mockmvc.adoc

+7-7
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@
55
MockMvc provides support for testing Spring MVC applications. It performs full Spring MVC
66
request handling but via mock request and response objects instead of a running server.
77

8-
MockMvc can be used on its own to perform requests and verify responses responses using
9-
Hamcrest, or through `MockMvcTester` that provides a fluent API using AssertJ. Finally,
10-
it can also be used through the xref:testing/webtestclient.adoc[WebTestClient] where
11-
MockMvc is plugged in as the server to handle requests with. The advantage of
12-
`WebTestClient` is the option to work with higher level objects instead of raw data as
13-
well as the ability to switch to full, end-to-end HTTP tests against a live server and
14-
use the same test API.
8+
MockMvc can be used on its own to perform requests and verify responses using Hamcrest or
9+
through `MockMvcTester` which provides a fluent API using AssertJ. It can also be used
10+
through the xref:testing/webtestclient.adoc[WebTestClient] where MockMvc is plugged in as
11+
the server to handle requests. The advantage of using `WebTestClient` is that it provides
12+
you the option of working with higher level objects instead of raw data as well as the
13+
ability to switch to full, end-to-end HTTP tests against a live server and use the same
14+
test API.
1515

1616

framework-docs/modules/ROOT/pages/testing/mockmvc/overview.adoc

+13-13
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,21 @@
44

55
You can write plain unit tests for Spring MVC by instantiating a controller, injecting it
66
with dependencies, and calling its methods. However such tests do not verify request
7-
mappings, data binding, message conversion, type conversion, validation, and nor
8-
do they involve any of the supporting `@InitBinder`, `@ModelAttribute`, or
7+
mappings, data binding, message conversion, type conversion, or validation and also do
8+
not involve any of the supporting `@InitBinder`, `@ModelAttribute`, or
99
`@ExceptionHandler` methods.
1010

11-
`MockMvc` aims to provide more complete testing for Spring MVC controllers without a
12-
running server. It does that by invoking the `DispatcherServlet` and passing
13-
xref:testing/unit.adoc#mock-objects-servlet["`mock`" implementations of the Servlet API] from the
14-
`spring-test` module which replicates the full Spring MVC request handling without
15-
a running server.
11+
`MockMvc` aims to provide more complete testing support for Spring MVC controllers
12+
without a running server. It does that by invoking the `DispatcherServlet` and passing
13+
xref:testing/unit.adoc#mock-objects-servlet["mock" implementations of the Servlet API]
14+
from the `spring-test` module which replicates the full Spring MVC request handling
15+
without a running server.
1616

17-
MockMvc is a server side test framework that lets you verify most of the functionality
18-
of a Spring MVC application using lightweight and targeted tests. You can use it on
19-
its own to perform requests and to verify responses using Hamcrest, or through
20-
`MockMvcTester` that provides a fluent API using AssertJ. Finally, you can also use it
21-
through the xref:testing/webtestclient.adoc[WebTestClient] API with MockMvc plugged in
22-
as the server to handle requests with.
17+
MockMvc is a server-side test framework that lets you verify most of the functionality of
18+
a Spring MVC application using lightweight and targeted tests. You can use it on its own
19+
to perform requests and to verify responses using Hamcrest or through `MockMvcTester`
20+
which provides a fluent API using AssertJ. You can also use it through the
21+
xref:testing/webtestclient.adoc[WebTestClient] API with MockMvc plugged in as the server
22+
to handle requests.
2323

2424

framework-docs/modules/ROOT/pages/testing/mockmvc/resources.adoc

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
= Further Examples
33
:page-section-summary-toc: 1
44

5-
The framework's own tests include
5+
The framework's own test suite includes
66
{spring-framework-code}/spring-test/src/test/java/org/springframework/test/web/servlet/samples[
77
many sample tests] intended to show how to use MockMvc on its own or through the
88
{spring-framework-code}/spring-test/src/test/java/org/springframework/test/web/servlet/samples/client[

framework-docs/modules/ROOT/pages/testing/mockmvc/setup-options.adoc

+14-11
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,31 @@
11
[[mockmvc-server-setup-options]]
22
= Setup Options
33

4-
MockMvc can be setup in one of two ways. One is to point directly to the controllers you
5-
want to test and programmatically configure Spring MVC infrastructure. The second is to
6-
point to Spring configuration with Spring MVC and controller infrastructure in it.
4+
MockMvc can be set up in one of two ways.
5+
6+
`WebApplicationContext` ::
7+
Point to Spring configuration with Spring MVC and controller infrastructure in it.
8+
Standalone ::
9+
Point directly to the controllers you want to test and programmatically configure Spring
10+
MVC infrastructure.
711

812
Which setup option should you use?
913

10-
The use of an `ApplicationContext` loads your actual Spring MVC configuration, resulting
11-
The `WebApplicationContext`-based test loads your actual Spring MVC configuration,
12-
resulting in a more complete integration test. Since the TestContext framework caches
13-
the loaded Spring configuration, it helps keep tests running fast, even as you introduce
14-
more tests in your test suite using the same configuration. Furthermore, you can
15-
override services used by your controller using `@MockitoBean` to remain focused on
14+
A `WebApplicationContext`-based test loads your actual Spring MVC configuration,
15+
resulting in a more complete integration test. Since the TestContext framework caches the
16+
loaded Spring configuration, it helps keep tests running fast, even as you introduce more
17+
tests in your test suite using the same configuration. Furthermore, you can override
18+
services used by your controller using `@MockitoBean` or `@TestBean` to remain focused on
1619
testing the web layer.
1720

18-
The standalone test, on the other hand, is a little closer to a unit test. It tests one
21+
A standalone test, on the other hand, is a little closer to a unit test. It tests one
1922
controller at a time. You can manually inject the controller with mock dependencies, and
2023
it does not involve loading Spring configuration. Such tests are more focused on style
2124
and make it easier to see which controller is being tested, whether any specific Spring
2225
MVC configuration is required to work, and so on. The standalone setup is also a very
2326
convenient way to write ad-hoc tests to verify specific behavior or to debug an issue.
2427

25-
As with most "`integration versus unit testing`" debates, there is no right or wrong
28+
As with most "integration versus unit testing" debates, there is no right or wrong
2629
answer. However, using standalone tests does imply the need for additional integration
2730
tests to verify your Spring MVC configuration. Alternatively, you can write all your
2831
tests with a `WebApplicationContext`, so that they always test against your actual Spring

framework-docs/modules/ROOT/pages/testing/mockmvc/vs-end-to-end-integration-tests.adoc

+7-7
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ The easiest way to think about this is by starting with a blank `MockHttpServlet
1010
Whatever you add to it is what the request becomes. Things that may catch you by surprise
1111
are that there is no context path by default; no `jsessionid` cookie; no forwarding,
1212
error, or async dispatches; and, therefore, no actual JSP rendering. Instead,
13-
"`forwarded`" and "`redirected`" URLs are saved in the `MockHttpServletResponse` and can
13+
"forwarded" and "redirected" URLs are saved in the `MockHttpServletResponse` and can
1414
be asserted with expectations.
1515

1616
This means that, if you use JSPs, you can verify the JSP page to which the request was
1717
forwarded, but no HTML is rendered. In other words, the JSP is not invoked. Note,
18-
however, that all other rendering technologies that do not rely on forwarding, such as
18+
however, that all other rendering technologies which do not rely on forwarding, such as
1919
Thymeleaf and Freemarker, render HTML to the response body as expected. The same is true
2020
for rendering JSON, XML, and other formats through `@ResponseBody` methods.
2121

@@ -30,17 +30,17 @@ testing, but they are a little closer to it. For example, you can isolate the we
3030
by injecting mocked services into controllers, in which case you are testing the web
3131
layer only through the `DispatcherServlet` but with actual Spring configuration, as you
3232
might test the data access layer in isolation from the layers above it. Also, you can use
33-
the stand-alone setup, focusing on one controller at a time and manually providing the
33+
the standalone setup, focusing on one controller at a time and manually providing the
3434
configuration required to make it work.
3535

3636
Another important distinction when using Spring MVC Test is that, conceptually, such
37-
tests are the server-side, so you can check what handler was used, if an exception was
38-
handled with a HandlerExceptionResolver, what the content of the model is, what binding
37+
tests are server-side tests, so you can check what handler was used, if an exception was
38+
handled with a `HandlerExceptionResolver`, what the content of the model is, what binding
3939
errors there were, and other details. That means that it is easier to write expectations,
4040
since the server is not an opaque box, as it is when testing it through an actual HTTP
41-
client. This is generally an advantage of classic unit testing: It is easier to write,
41+
client. This is generally an advantage of classic unit testing: it is easier to write,
4242
reason about, and debug but does not replace the need for full integration tests. At the
4343
same time, it is important not to lose sight of the fact that the response is the most
44-
important thing to check. In short, there is room here for multiple styles and strategies
44+
important thing to check. In short, there is room for multiple styles and strategies
4545
of testing even within the same project.
4646

0 commit comments

Comments
 (0)