From 8f8a4af001176e35fbe332d2c532d400d73246a8 Mon Sep 17 00:00:00 2001 From: Tetsuya Hasegawa Date: Sun, 5 May 2019 07:20:58 +0900 Subject: [PATCH 1/2] Document the pros and cons of MockMvc Update the MockMvc documentation to provide more details about the pros and cons of such an approach, specifically calling out the difference with error page handling. See gh-16718 --- .../src/main/asciidoc/spring-boot-features.adoc | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/spring-boot-project/spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc b/spring-boot-project/spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc index e37b99a008..53acfcf764 100644 --- a/spring-boot-project/spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc +++ b/spring-boot-project/spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc @@ -5639,6 +5639,12 @@ Alternatively, you can configure a {spring-framework-docs}testing.html#webtestcl include::{code-examples}/test/web/MockWebTestClientExampleTests.java[tag=test-mock-web-test-client] ---- +Testing within a mocked environment enables fast runs as it does not require the cost of setting up a full Servlet container. +Although this works fine in most cases, you cannot test situations where the servlet container takes precedence. +For example, Spring Boot's error handling is based on Servlet container’s error mappings. +Therefore, exceptions behave differently in the container-less mock environment than the real environment. +If you need to test the precise format of the error response, test with a fully running server as follows. + [[boot-features-testing-spring-boot-applications-testing-with-running-server]] From 624c118cdf69e75f0b49b1af62fc0be27a08736c Mon Sep 17 00:00:00 2001 From: Phillip Webb Date: Tue, 3 Sep 2019 21:01:23 -0700 Subject: [PATCH 2/2] Polish "Document the pros and cons of MockMvc" See gh-16718 --- .../src/main/asciidoc/spring-boot-features.adoc | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/spring-boot-project/spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc b/spring-boot-project/spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc index 53acfcf764..3275bbdcc5 100644 --- a/spring-boot-project/spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc +++ b/spring-boot-project/spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc @@ -5639,11 +5639,15 @@ Alternatively, you can configure a {spring-framework-docs}testing.html#webtestcl include::{code-examples}/test/web/MockWebTestClientExampleTests.java[tag=test-mock-web-test-client] ---- -Testing within a mocked environment enables fast runs as it does not require the cost of setting up a full Servlet container. -Although this works fine in most cases, you cannot test situations where the servlet container takes precedence. -For example, Spring Boot's error handling is based on Servlet container’s error mappings. -Therefore, exceptions behave differently in the container-less mock environment than the real environment. -If you need to test the precise format of the error response, test with a fully running server as follows. +[TIP] +==== +Testing within a mocked environment is usually faster than running with a full Servlet container. +However, since mocking occurs at the Spring MVC layer, code that relies on on lower level Servlet concerns cannot be directly tested with MockMvc. + +For example, Spring Boot's error handling is based on the "`error page`" support provided by the Servlet container. +This means that, whilst you can test your MVC layer throws and handles exceptions as expected, you cannot directly test that a specific <<#boot-features-error-handling-custom-error-pages, custom error page>> is rendered. +If you need to tests these lower-level concerns, you can start a fully running server as described in the next section. +====