From: Kai Moritz Date: Sat, 19 Sep 2020 11:54:30 +0000 (+0200) Subject: TODO... Passt der neue Test noch? X-Git-Url: http://juplo.de/gitweb/?a=commitdiff_plain;h=7bf76e7ab569dd3392b3904a734bb1895bd219a2;p=demos%2Ftesting TODO... Passt der neue Test noch? Added a working component-test for exceptions, thrown outside of controllers --- diff --git a/src/test/java/de/juplo/demo/ExceptionHandlingApplicationTests.java b/src/test/java/de/juplo/demo/ExceptionHandlingApplicationTests.java index b450f17..b5e346f 100644 --- a/src/test/java/de/juplo/demo/ExceptionHandlingApplicationTests.java +++ b/src/test/java/de/juplo/demo/ExceptionHandlingApplicationTests.java @@ -9,6 +9,7 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; import org.springframework.boot.test.mock.mockito.MockBean; import org.springframework.test.web.servlet.MockMvc; +import org.springframework.web.util.NestedServletException; import java.net.URI; import java.util.Optional; @@ -17,6 +18,7 @@ import static org.mockito.AdditionalMatchers.geq; import static org.mockito.AdditionalMatchers.lt; import static org.mockito.ArgumentMatchers.*; import static org.mockito.Mockito.*; +import static org.junit.jupiter.api.Assertions.assertThrows; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; @@ -87,6 +89,20 @@ class ExceptionHandlingApplicationTests { .andExpect(status().isBadRequest()); } + @Test + void test503_WORKING() throws Exception { + // MockMvc throws the exception, because it is thrown outside + // of the context of the called controller. Catching and handling + // of this exception would require, to have a reale servlet container + // in place, which would be against the intention of mocking away + // everything except the core MVC functionallity. + // Hence, throwing exceptions, which are thrown outside of the context + // of the controller IS the expected behaviour here! + assertThrows( + NestedServletException.class, + () -> mvc.perform(get(URI.create("http://FOO/?template=foo")))); + } + @Test void test503() throws Exception { mvc