From: Kai Moritz Date: Sat, 19 Sep 2020 08:24:44 +0000 (+0200) Subject: Refined the example, to clearify the intent X-Git-Url: http://juplo.de/gitweb/?a=commitdiff_plain;h=64108e90d14f6e85c76c463b460ba92307ef15cd;p=demos%2Ftesting 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 --- diff --git a/src/main/java/de/juplo/demo/ExampleController.java b/src/main/java/de/juplo/demo/ExampleController.java new file mode 100644 index 0000000..b6e75d8 --- /dev/null +++ b/src/main/java/de/juplo/demo/ExampleController.java @@ -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 index f7b12ff..0000000 --- a/src/main/java/de/juplo/demo/ThymeleafController.java +++ /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 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()); } - }