Skip to content

Commit de3b09b

Browse files
committed
- Added test code for the article "Returning Custom Status Codes from Spring MVC Controllers"
1 parent 453d995 commit de3b09b

1 file changed

Lines changed: 43 additions & 0 deletions

File tree

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

0 commit comments

Comments
 (0)