File tree Expand file tree Collapse file tree 1 file changed +40
-0
lines changed
src/test/java/com/olegshan/controllers Expand file tree Collapse file tree 1 file changed +40
-0
lines changed Original file line number Diff line number Diff line change
1
+ package com .olegshan .controllers ;
2
+
3
+ import org .junit .Before ;
4
+ import org .junit .Test ;
5
+ import org .junit .runner .RunWith ;
6
+ import org .mockito .Mock ;
7
+ import org .mockito .runners .MockitoJUnitRunner ;
8
+ import org .springframework .test .web .servlet .MockMvc ;
9
+ import org .springframework .test .web .servlet .setup .MockMvcBuilders ;
10
+
11
+ import static org .mockito .Mockito .when ;
12
+ import static org .springframework .test .web .servlet .request .MockMvcRequestBuilders .get ;
13
+ import static org .springframework .test .web .servlet .result .MockMvcResultMatchers .*;
14
+
15
+ @ RunWith (MockitoJUnitRunner .class )
16
+ public class ErrorHandlerTest {
17
+
18
+ @ Mock
19
+ ParseController parseController ;
20
+
21
+ private MockMvc mockMvc ;
22
+
23
+ @ Before
24
+ public void setUp () throws Exception {
25
+ mockMvc = MockMvcBuilders .standaloneSetup (parseController )
26
+ .setControllerAdvice (new ErrorHandler ())
27
+ .build ();
28
+ }
29
+
30
+ @ Test
31
+ public void unexpectedExceptionsAreCaught () throws Exception {
32
+
33
+ when (parseController .about ()).thenThrow (new RuntimeException ("Unexpected exception" ));
34
+
35
+ mockMvc .perform (get ("/about" ))
36
+ .andExpect (status ().isOk ())
37
+ .andExpect (view ().name ("exception" ))
38
+ .andExpect (model ().attribute ("errorMessage" , "Unexpected exception" ));
39
+ }
40
+ }
You can’t perform that action at this time.
0 commit comments