Refined the example, to clearify the intent
authorKai Moritz <kai@juplo.de>
Sat, 19 Sep 2020 08:24:44 +0000 (10:24 +0200)
committerKai Moritz <kai@juplo.de>
Sun, 27 Sep 2020 08:56:43 +0000 (10:56 +0200)
* 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

src/main/java/de/juplo/demo/ExampleController.java [new file with mode: 0644]
src/main/java/de/juplo/demo/ThymeleafController.java [deleted file]
src/main/resources/templates/a.html [new file with mode: 0644]
src/main/resources/templates/b.html [new file with mode: 0644]
src/main/resources/templates/local.html [deleted file]
src/main/resources/templates/remote.html [deleted file]
src/test/java/de/juplo/demo/ExceptionHandlingApplicationTests.java

diff --git a/src/main/java/de/juplo/demo/ExampleController.java b/src/main/java/de/juplo/demo/ExampleController.java
new file mode 100644 (file)
index 0000000..b6e75d8
--- /dev/null
@@ -0,0 +1,38 @@
+package de.juplo.demo;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.http.HttpStatus;
+import org.springframework.stereotype.Controller;
+import org.springframework.ui.Model;
+import org.springframework.web.bind.annotation.ExceptionHandler;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RequestParam;
+import org.springframework.web.bind.annotation.ResponseStatus;
+import org.thymeleaf.exceptions.TemplateInputException;
+
+@Controller
+public class ExampleController
+{
+    private final static Logger LOG =
+            LoggerFactory.getLogger(ExampleController.class);
+
+
+    @RequestMapping("/")
+    public String controller(
+            @RequestParam(defaultValue = "a") String template,
+            Model model
+    )
+    {
+        model.addAttribute("template", template);
+        return template;
+    }
+
+    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
+    @ExceptionHandler(TemplateInputException.class)
+    public void templateInputException(TemplateInputException e)
+    {
+        LOG.error("{}: {}", HttpStatus.INTERNAL_SERVER_ERROR, e.getMessage());
+    }
+}
+
diff --git a/src/main/java/de/juplo/demo/ThymeleafController.java b/src/main/java/de/juplo/demo/ThymeleafController.java
deleted file mode 100644 (file)
index f7b12ff..0000000
+++ /dev/null
@@ -1,38 +0,0 @@
-package de.juplo.demo;
-
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-import org.springframework.http.HttpStatus;
-import org.springframework.stereotype.Controller;
-import org.springframework.ui.Model;
-import org.springframework.web.bind.annotation.ExceptionHandler;
-import org.springframework.web.bind.annotation.RequestMapping;
-import org.springframework.web.bind.annotation.RequestParam;
-import org.springframework.web.bind.annotation.ResponseStatus;
-import org.thymeleaf.exceptions.TemplateInputException;
-
-@Controller
-public class ThymeleafController
-{
-    private final static Logger LOG =
-            LoggerFactory.getLogger(ThymeleafController.class);
-
-
-    @RequestMapping("/controller.html")
-    public String controller(
-            @RequestParam String template,
-            Model model
-    )
-    {
-        model.addAttribute("template", template);
-        return template;
-    }
-
-    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
-    @ExceptionHandler(TemplateInputException.class)
-    public void templateInputException(TemplateInputException e)
-    {
-        LOG.error("{}: {}", HttpStatus.INTERNAL_SERVER_ERROR, e.getMessage());
-    }
-}
-
diff --git a/src/main/resources/templates/a.html b/src/main/resources/templates/a.html
new file mode 100644 (file)
index 0000000..60de028
--- /dev/null
@@ -0,0 +1,28 @@
+<!DOCTYPE HTML>
+<html xmlns:th="http://www.thymeleaf.org">
+  <head>
+    <title>Testing Exception-Handling - Template A</title>
+    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
+  </head>
+  <body>
+    <h1>Template A</h1>
+    <div>
+      <p><strong th:text="'Serving with template ::' + ${template} + '::!'">TEXT</strong></p>
+      <p>
+        Type in <strong><code>a</code></strong> or <strong><code>b</code></strong>
+        for an existing template (no exception). Type in any other string for a
+        non-existent template: Thymeleaf will throw a
+        <strong><code>TemplateInputException</code></strong>!
+      </p>
+    </div>
+    <div>
+      <form action="#" th:action="@{/}" method="get">
+        <div>
+          <label for="path">Remote-Path to fetch:</label>
+          <input type="text" name="template" value="a" th:value="${template}"/>
+        </div>
+        <button type="submit">Submit</button>
+      </form>
+    </div>
+  </body>
+</html>
diff --git a/src/main/resources/templates/b.html b/src/main/resources/templates/b.html
new file mode 100644 (file)
index 0000000..c89c69f
--- /dev/null
@@ -0,0 +1,28 @@
+<!DOCTYPE HTML>
+<html xmlns:th="http://www.thymeleaf.org">
+  <head>
+    <title>Testing Exception-Handling - Template B</title>
+    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
+  </head>
+  <body>
+    <h1>Template B</h1>
+    <div>
+      <p><strong th:text="'Serving with template ::' + ${template} + '::!'">TEXT</strong></p>
+      <p>
+        Type in <strong><code>a</code></strong> or <strong><code>b</code></strong>
+        for an existing template (no exception). Type in any other string for a
+        non-existent template: Thymeleaf will throw a
+        <strong><code>TemplateInputException</code></strong>!
+      </p>
+    </div>
+    <div>
+      <form action="#" th:action="@{/}" method="get">
+        <div>
+          <label for="path">Remote-Path to fetch:</label>
+          <input type="text" name="template" value="a" th:value="${template}"/>
+        </div>
+        <button type="submit">Submit</button>
+      </form>
+    </div>
+  </body>
+</html>
diff --git a/src/main/resources/templates/local.html b/src/main/resources/templates/local.html
deleted file mode 100644 (file)
index 6d62403..0000000
+++ /dev/null
@@ -1,11 +0,0 @@
-<!DOCTYPE HTML>
-<html xmlns:th="http://www.thymeleaf.org">
-<head>
-  <title>ThymeleafTest - Local Template</title>
-  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
-</head>
-<body>
-  <h1>This Is The Local Template</h1>
-  <p th:text="'Serving with template ::' + ${template} + '::!'" />
-</body>
-</html>
diff --git a/src/main/resources/templates/remote.html b/src/main/resources/templates/remote.html
deleted file mode 100644 (file)
index 573a45f..0000000
+++ /dev/null
@@ -1,11 +0,0 @@
-<!DOCTYPE HTML>
-<html xmlns:th="http://www.thymeleaf.org">
-<head>
-  <title>ThymeleafTest - Remote Template</title>
-  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
-</head>
-<body>
-  <h1>This Is The Remote Template</h1>
-  <p th:text="'Serving with template ::' + ${template} + '::!'" />
-</body>
-</html>
index 71a415a..9e6d753 100644 (file)
@@ -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());
        }
-
 }