Skip to content

Commit dc715a0

Browse files
committed
Update Spring MVC Test reference
Add section on Spring MVC TEst vs full integation testing and provide reference to Spring Boot's @WebIntegrationTest as an alternative. Issue: SPR-13169
1 parent 9bb29fb commit dc715a0

File tree

1 file changed

+57
-1
lines changed

1 file changed

+57
-1
lines changed

src/asciidoc/testing.adoc

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3698,6 +3698,15 @@ __Spring MVC Test__ also provides client-side support for testing code that uses
36983698
the `RestTemplate`. Client-side tests mock the server responses and also do not
36993699
require a running server.
37003700

3701+
[TIP]
3702+
====
3703+
Spring Boot provides an option to write full, end-to-end integration tests that include
3704+
a running server. If this is your goal please have a look at the
3705+
http://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-testing.html#boot-features-testing-spring-boot-applications[Spring Boot reference page].
3706+
For more on the difference with end-to-end integration tests see
3707+
<<spring-mvc-test-vs-end-to-end-integration-tests>>.
3708+
====
3709+
37013710

37023711

37033712
[[spring-mvc-test-server]]
@@ -3717,7 +3726,8 @@ __Spring MVC Test__ builds on the familiar "mock" implementations of the Servlet
37173726
available in the `spring-test` module. This allows performing requests and generating
37183727
responses without the need for running in a Servlet container. For the most part
37193728
everything should work as it does at runtime with a few notable exceptions as
3720-
explained further below. Here is an example of using Spring MVC Test:
3729+
explained in <<spring-mvc-test-vs-end-to-end-integration-tests>>.
3730+
Here is an example of using Spring MVC Test:
37213731

37223732
[source,java,indent=0]
37233733
----
@@ -4074,6 +4084,52 @@ When setting up a `MockMvc`, you can register one or more `Filter` instances:
40744084
Registered filters will be invoked through `MockFilterChain` from `spring-test` and the
40754085
last filter will delegates to the `DispatcherServlet`.
40764086

4087+
[[spring-mvc-test-vs-end-to-end-integration-tests]]
4088+
===== Difference With End-to-End Integration Tests
4089+
4090+
As mentioned earlier __Spring MVC Test__ is built on the Servlet API mock objects from
4091+
the `spring-test` module and does not rely on a running Servlet container. Therefore
4092+
there are some important differences compared to full end-to-end integration tests
4093+
with an actual client and server running.
4094+
4095+
The easiest way to think about this is starting with a blank `MockHttpServletRequest`.
4096+
Whatever you add to it is what the request will be. The things that may catch you out are
4097+
there is no context path by default, no jsessionid cookie, no forwarding, error, or async
4098+
dispatches, and therefore no actual JSP rendering. Instead "forwarded" and "redirected"
4099+
URLs are saved in the `MockHttpServletResponse` and can be asserted with expectations.
4100+
4101+
This means if you are using JSPs you can verify the JSP page to which the request was
4102+
forwarded but there won't be any HTML rendered. Note however that all other rendering
4103+
technologies that don't rely on forwarding such as Thymeleaf, Freemarker, Velocity
4104+
will render HTML to the response body as expected. The same is true for rendering JSON,
4105+
XML and others via `@ResponseBody` methods.
4106+
4107+
Alternatively you may consider the full end-to-end integration testing support from
4108+
Spring Boot via `@WebIntegrationTest`. See the
4109+
http://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-testing.html#boot-features-testing-spring-boot-applications[Spring Boot reference].
4110+
4111+
There are pros and cons for each. The options provided in __Spring MVC Test__
4112+
are different stops on the scale from classic unit to full integration tests.
4113+
To be sure none of the options in Spring MVC Test are classic unit tests but they are a
4114+
little closer to it. For example you can isolate the service layer with mocks
4115+
injected into controllers and then you're testing the web layer only through
4116+
the `DispatcherServlet` and with actual Spring configuration, just like you might test
4117+
the database layer in isolation of the layers above. Or you could be using the
4118+
standalone setup focusing on one controller at a time and manually providing the
4119+
configuration required to make it work.
4120+
4121+
Another important distinction when using __Spring MVC Test__ is that conceptually such
4122+
tests are on the inside of the server-side so you can check what handler was used,
4123+
if an exception was handled with a HandlerExceptionResolver, what the content of the
4124+
model is, what binding errors there were, etc. That means it's easier to write
4125+
expectations since the server is not a black box as it is when testing it through
4126+
an actual HTTP client. This is generally the advantage of classic unit testing that it's
4127+
easier to write, reason about, and debug but does not replace the need for full
4128+
integration tests. At the same time it's important not to lose sight of the fact
4129+
the response is the most important thing to check. In short there is room here for
4130+
multiple styles and strategies of testing even in the same project.
4131+
4132+
40774133
[[spring-mvc-test-server-resources]]
40784134
===== Further Server-Side Test Examples
40794135
The framework's own tests include

0 commit comments

Comments
 (0)