From 64108e90d14f6e85c76c463b460ba92307ef15cd Mon Sep 17 00:00:00 2001 From: Kai Moritz Date: Sat, 19 Sep 2020 10:24:44 +0200 Subject: [PATCH] Refined the example, to clearify the intent * Removed unnecessary Annotation from test * Renamed and pimped up the example templates * Splitted up the test in separate units * Remapped the controller to the root of the web-context * Switched to @WebMvcTest, to load only the controller-layer --- ...Controller.java => ExampleController.java} | 8 +++--- src/main/resources/templates/a.html | 28 +++++++++++++++++++ src/main/resources/templates/b.html | 28 +++++++++++++++++++ src/main/resources/templates/local.html | 11 -------- src/main/resources/templates/remote.html | 11 -------- .../ExceptionHandlingApplicationTests.java | 21 +++++++------- 6 files changed, 70 insertions(+), 37 deletions(-) rename src/main/java/de/juplo/demo/{ThymeleafController.java => ExampleController.java} (84%) create mode 100644 src/main/resources/templates/a.html create mode 100644 src/main/resources/templates/b.html delete mode 100644 src/main/resources/templates/local.html delete mode 100644 src/main/resources/templates/remote.html diff --git a/src/main/java/de/juplo/demo/ThymeleafController.java b/src/main/java/de/juplo/demo/ExampleController.java similarity index 84% rename from src/main/java/de/juplo/demo/ThymeleafController.java rename to src/main/java/de/juplo/demo/ExampleController.java index f7b12ff..b6e75d8 100644 --- a/src/main/java/de/juplo/demo/ThymeleafController.java +++ b/src/main/java/de/juplo/demo/ExampleController.java @@ -12,15 +12,15 @@ import org.springframework.web.bind.annotation.ResponseStatus; import org.thymeleaf.exceptions.TemplateInputException; @Controller -public class ThymeleafController +public class ExampleController { private final static Logger LOG = - LoggerFactory.getLogger(ThymeleafController.class); + LoggerFactory.getLogger(ExampleController.class); - @RequestMapping("/controller.html") + @RequestMapping("/") public String controller( - @RequestParam String template, + @RequestParam(defaultValue = "a") String template, Model model ) { diff --git a/src/main/resources/templates/a.html b/src/main/resources/templates/a.html new file mode 100644 index 0000000..60de028 --- /dev/null +++ b/src/main/resources/templates/a.html @@ -0,0 +1,28 @@ + + + + Testing Exception-Handling - Template A + + + +

Template A

+
+

TEXT

+

+ Type in a or b + for an existing template (no exception). Type in any other string for a + non-existent template: Thymeleaf will throw a + TemplateInputException! +

+
+
+
+
+ + +
+ +
+
+ + diff --git a/src/main/resources/templates/b.html b/src/main/resources/templates/b.html new file mode 100644 index 0000000..c89c69f --- /dev/null +++ b/src/main/resources/templates/b.html @@ -0,0 +1,28 @@ + + + + Testing Exception-Handling - Template B + + + +

Template B

+
+

TEXT

+

+ Type in a or b + for an existing template (no exception). Type in any other string for a + non-existent template: Thymeleaf will throw a + TemplateInputException! +

+
+
+
+
+ + +
+ +
+
+ + diff --git a/src/main/resources/templates/local.html b/src/main/resources/templates/local.html deleted file mode 100644 index 6d62403..0000000 --- a/src/main/resources/templates/local.html +++ /dev/null @@ -1,11 +0,0 @@ - - - - ThymeleafTest - Local Template - - - -

This Is The Local Template

-

- - diff --git a/src/main/resources/templates/remote.html b/src/main/resources/templates/remote.html deleted file mode 100644 index 573a45f..0000000 --- a/src/main/resources/templates/remote.html +++ /dev/null @@ -1,11 +0,0 @@ - - - - ThymeleafTest - Remote Template - - - -

This Is The Remote Template

-

- - diff --git a/src/test/java/de/juplo/demo/ExceptionHandlingApplicationTests.java b/src/test/java/de/juplo/demo/ExceptionHandlingApplicationTests.java index 71a415a..9e6d753 100644 --- a/src/test/java/de/juplo/demo/ExceptionHandlingApplicationTests.java +++ b/src/test/java/de/juplo/demo/ExceptionHandlingApplicationTests.java @@ -4,9 +4,7 @@ import org.junit.jupiter.api.Test; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; -import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureWebMvc; -import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; import org.springframework.test.web.servlet.MockMvc; import java.net.URI; @@ -14,9 +12,7 @@ import java.net.URI; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; -@SpringBootTest -@AutoConfigureWebMvc -@AutoConfigureMockMvc +@WebMvcTest(ExampleController.class) class ExceptionHandlingApplicationTests { private final static Logger LOG = LoggerFactory.getLogger(ExceptionHandlingApplicationTests.class); @@ -30,13 +26,17 @@ class ExceptionHandlingApplicationTests { } @Test - void requests() throws Exception { + void test200() throws Exception { mvc - .perform(get(URI.create("http://test/controller.html?template=remote"))) + .perform(get(URI.create("http://FOO/?template=a"))) .andExpect(status().isOk()); mvc - .perform(get(URI.create("http://test/controller.html?template=local"))) + .perform(get(URI.create("http://FOO/?template=b"))) .andExpect(status().isOk()); + } + + @Test + void test503_NOT_WORKING() throws Exception { // The expected behaviour of the following test is, that the // TemplateInputException, that is thrown by Thymeleaf because of the non-existent // template-resource, is catched and reported as 503 Internal Server Error, as it @@ -45,8 +45,7 @@ class ExceptionHandlingApplicationTests { // Instead, the exception bubbles up, becomes wrapped in a NestedServletException // and is thrown in the call to perform()! mvc - .perform(get(URI.create("http://test/controller.html?template=foo"))) + .perform(get(URI.create("http://FOO/?template=foo"))) .andExpect(status().isInternalServerError()); } - } -- 2.20.1