From: Kai Moritz Date: Sat, 19 Sep 2020 08:11:41 +0000 (+0200) Subject: Renamed and repackaged the example for the blog-article X-Git-Url: http://juplo.de/gitweb/?a=commitdiff_plain;h=6cc8cb552e6a9adf0a5c70db18280f1baa01f1d9;p=demos%2Ftesting Renamed and repackaged the example for the blog-article --- diff --git a/pom.xml b/pom.xml index 36cdb2f..e0b19e8 100644 --- a/pom.xml +++ b/pom.xml @@ -8,11 +8,11 @@ 2.3.4.RELEASE - de.juplo - thymeleafe-test + de.juplo.demo + exception-handling 0.0.1-SNAPSHOT - Thymeleaf-Test - Standalone version of the ThymeleafTest in http-resources + Testing Exception-Handling + Example-project, that showcases, how to test exception-handling in the springframework 11 diff --git a/src/main/java/de/juplo/TestController.java b/src/main/java/de/juplo/TestController.java deleted file mode 100644 index 5a4d148..0000000 --- a/src/main/java/de/juplo/TestController.java +++ /dev/null @@ -1,38 +0,0 @@ -package de.juplo; - -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 TestController -{ - private final static Logger LOG = - LoggerFactory.getLogger(TestController.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/java/de/juplo/ThymeleafTestApplication.java b/src/main/java/de/juplo/ThymeleafTestApplication.java deleted file mode 100644 index aee3821..0000000 --- a/src/main/java/de/juplo/ThymeleafTestApplication.java +++ /dev/null @@ -1,13 +0,0 @@ -package de.juplo; - -import org.springframework.boot.SpringApplication; -import org.springframework.boot.autoconfigure.SpringBootApplication; - -@SpringBootApplication -public class ThymeleafTestApplication { - - public static void main(String[] args) { - SpringApplication.run(ThymeleafTestApplication.class, args); - } - -} diff --git a/src/main/java/de/juplo/demo/ExceptionHandlingApplication.java b/src/main/java/de/juplo/demo/ExceptionHandlingApplication.java new file mode 100644 index 0000000..584f702 --- /dev/null +++ b/src/main/java/de/juplo/demo/ExceptionHandlingApplication.java @@ -0,0 +1,13 @@ +package de.juplo.demo; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class ExceptionHandlingApplication { + + public static void main(String[] args) { + SpringApplication.run(ExceptionHandlingApplication.class, args); + } + +} diff --git a/src/main/java/de/juplo/demo/ThymeleafController.java b/src/main/java/de/juplo/demo/ThymeleafController.java new file mode 100644 index 0000000..f7b12ff --- /dev/null +++ b/src/main/java/de/juplo/demo/ThymeleafController.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 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/test/java/de/juplo/ThymeleafTestApplicationTests.java b/src/test/java/de/juplo/ThymeleafTestApplicationTests.java deleted file mode 100644 index adb1e24..0000000 --- a/src/test/java/de/juplo/ThymeleafTestApplicationTests.java +++ /dev/null @@ -1,53 +0,0 @@ -package de.juplo; - -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.test.web.servlet.MockMvc; -import org.springframework.test.web.servlet.MvcResult; - -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 -class ThymeleafTestApplicationTests { - private final static Logger LOG = - LoggerFactory.getLogger(ThymeleafTestApplicationTests.class); - - @Autowired - MockMvc mvc; - - - @Test - void contextLoads() throws Exception { - } - - @Test - void requests() throws Exception { - mvc - .perform(get(URI.create("http://test/controller.html?template=remote"))) - .andExpect(status().isOk()); - mvc - .perform(get(URI.create("http://test/controller.html?template=local"))) - .andExpect(status().isOk()); - // 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 - // happens in the real Spring-MVC environment, when you start the web-server and - // trigger the error manually. - // 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"))) - .andExpect(status().isInternalServerError()); - } - -} diff --git a/src/test/java/de/juplo/demo/ExceptionHandlingApplicationTests.java b/src/test/java/de/juplo/demo/ExceptionHandlingApplicationTests.java new file mode 100644 index 0000000..71a415a --- /dev/null +++ b/src/test/java/de/juplo/demo/ExceptionHandlingApplicationTests.java @@ -0,0 +1,52 @@ +package de.juplo.demo; + +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.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 +class ExceptionHandlingApplicationTests { + private final static Logger LOG = + LoggerFactory.getLogger(ExceptionHandlingApplicationTests.class); + + @Autowired + MockMvc mvc; + + + @Test + void contextLoads() throws Exception { + } + + @Test + void requests() throws Exception { + mvc + .perform(get(URI.create("http://test/controller.html?template=remote"))) + .andExpect(status().isOk()); + mvc + .perform(get(URI.create("http://test/controller.html?template=local"))) + .andExpect(status().isOk()); + // 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 + // happens in the real Spring-MVC environment, when you start the web-server and + // trigger the error manually. + // 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"))) + .andExpect(status().isInternalServerError()); + } + +}