File tree Expand file tree Collapse file tree
spring-rest/src/test/java/org/baeldung/web/controller/status Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ package com .baeldung ;
2+
3+ import static org .springframework .test .web .servlet .request .MockMvcRequestBuilders .get ;
4+ import static org .springframework .test .web .servlet .result .MockMvcResultMatchers .status ;
5+
6+ import org .junit .Before ;
7+ import org .junit .Test ;
8+ import org .junit .runner .RunWith ;
9+ import org .springframework .beans .factory .annotation .Autowired ;
10+ import org .springframework .boot .test .SpringApplicationConfiguration ;
11+ import org .springframework .test .context .junit4 .SpringJUnit4ClassRunner ;
12+ import org .springframework .test .context .web .WebAppConfiguration ;
13+ import org .springframework .test .web .servlet .MockMvc ;
14+ import org .springframework .test .web .servlet .setup .MockMvcBuilders ;
15+ import org .springframework .web .context .WebApplicationContext ;
16+
17+ @ RunWith (SpringJUnit4ClassRunner .class )
18+ @ ContextConfiguration (classes = WebConfig .class )
19+ @ WebAppConfiguration
20+ public class ExampleControllerTest {
21+
22+ private MockMvc mockMvc ;
23+
24+ @ Autowired
25+ private WebApplicationContext webApplicationContext ;
26+
27+ @ Before
28+ public void setUp () {
29+ mockMvc = MockMvcBuilders .webAppContextSetup (webApplicationContext ).build ();
30+ }
31+
32+ @ Test
33+ public void whenGetRequestSentToController_thenReturnsStatusNotAcceptable () throws Exception {
34+ mockMvc .perform (get ("/controller" ))
35+ .andExpect (status ().isNotAcceptable ());
36+ }
37+
38+ @ Test
39+ public void whenGetRequestSentToException_thenReturnsStatusForbidden () throws Exception {
40+ mockMvc .perform (get ("/exception" ))
41+ .andExpect (status ().isForbidden ());
42+ }
43+ }
You can’t perform that action at this time.
0 commit comments