--- /dev/null
+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());
+ }
+}
+
+++ /dev/null
-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());
- }
-}
-
--- /dev/null
+<!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>
--- /dev/null
+<!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>
+++ /dev/null
-<!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>
+++ /dev/null
-<!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>
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;
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);
}
@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
// 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());
}
-
}