Skip to content

Commit 9788049

Browse files
committed
ErrorHandler test added
1 parent 55ceff2 commit 9788049

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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+
}

0 commit comments

Comments
 (0)