TODO... Passt der neue Test noch?
authorKai Moritz <kai@juplo.de>
Sat, 19 Sep 2020 11:54:30 +0000 (13:54 +0200)
committerKai Moritz <kai@juplo.de>
Mon, 28 Sep 2020 19:27:44 +0000 (21:27 +0200)
Added a working component-test for exceptions, thrown outside of controllers

src/test/java/de/juplo/demo/ExceptionHandlingApplicationTests.java

index b450f17..b5e346f 100644 (file)
@@ -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