|
1 | | -package org.baeldung.redirect; |
| 1 | +package org.baeldung.web.controller.redirect; |
2 | 2 |
|
3 | | -import static org.hamcrest.CoreMatchers.*; |
4 | | -import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; |
5 | | -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; |
6 | | -import static org.springframework.test.web.servlet.setup.MockMvcBuilders.*; |
| 3 | +import static org.hamcrest.CoreMatchers.is; |
| 4 | +import static org.hamcrest.CoreMatchers.nullValue; |
| 5 | +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; |
| 6 | +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.flash; |
| 7 | +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.forwardedUrl; |
| 8 | +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.model; |
| 9 | +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.redirectedUrl; |
| 10 | +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; |
| 11 | +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.view; |
| 12 | +import static org.springframework.test.web.servlet.setup.MockMvcBuilders.webAppContextSetup; |
7 | 13 |
|
8 | 14 | import org.junit.Before; |
9 | 15 | import org.junit.Test; |
|
16 | 22 | import org.springframework.web.context.WebApplicationContext; |
17 | 23 |
|
18 | 24 | @RunWith(SpringJUnit4ClassRunner.class) |
| 25 | +@ContextConfiguration("file:src/main/webapp/WEB-INF/api-servlet.xml") |
19 | 26 | @WebAppConfiguration |
20 | | -@ContextConfiguration("file:src/main/webapp/WEB-INF/mvc-dispatcher-servlet.xml") |
21 | 27 | public class RedirectControllerTest { |
22 | 28 |
|
23 | | - private MockMvc mockMvc; |
24 | | - |
25 | | - @Autowired |
26 | | - protected WebApplicationContext wac; |
27 | | - |
28 | | - @Before |
29 | | - public void setup() { |
30 | | - this.mockMvc = webAppContextSetup(this.wac).build(); |
31 | | - } |
32 | | - |
33 | | - @Test |
34 | | - public void whenRedirectOnUrlWithUsingXMLConfig_thenStatusRedirectionAndRedirectedOnUrl() throws Exception { |
35 | | - this.mockMvc.perform(get("/redirectWithXMLConfig")).andExpect(status().is3xxRedirection()).andExpect(view().name("RedirectedUrl")).andExpect(model().attribute("attribute", is("redirectWithXMLConfig"))) |
36 | | - .andExpect(redirectedUrl("redirectedUrl?attribute=redirectWithXMLConfig")); |
37 | | - } |
38 | | - |
39 | | - @Test |
40 | | - public void whenRedirectOnUrlWithUsingRedirectPrefix_thenStatusRedirectionAndRedirectedOnUrl() throws Exception { |
41 | | - this.mockMvc.perform(get("/redirectWithRedirectPrefix")).andExpect(status().is3xxRedirection()).andExpect(view().name("redirect:/redirectedUrl")).andExpect(model().attribute("attribute", is("redirectWithRedirectPrefix"))) |
42 | | - .andExpect(redirectedUrl("/redirectedUrl?attribute=redirectWithRedirectPrefix")); |
43 | | - } |
44 | | - |
45 | | - @Test |
46 | | - public void whenRedirectOnUrlWithUsingRedirectAttributes_thenStatusRedirectionAndRedirectedOnUrlAndAddedAttributeToFlashScope() throws Exception { |
47 | | - this.mockMvc.perform(get("/redirectWithRedirectAttributes")).andExpect(status().is3xxRedirection()).andExpect(flash().attribute("flashAttribute", is("redirectWithRedirectAttributes"))) |
48 | | - .andExpect(model().attribute("attribute", is("redirectWithRedirectAttributes"))).andExpect(model().attribute("flashAttribute", is(nullValue()))).andExpect(redirectedUrl("redirectedUrl?attribute=redirectWithRedirectAttributes")); |
49 | | - } |
50 | | - |
51 | | - @Test |
52 | | - public void whenRedirectOnUrlWithUsingRedirectView_thenStatusRedirectionAndRedirectedOnUrlAndAddedAttributeToFlashScope() throws Exception { |
53 | | - this.mockMvc.perform(get("/redirectWithRedirectView")).andExpect(status().is3xxRedirection()).andExpect(model().attribute("attribute", is("redirectWithRedirectView"))).andExpect(redirectedUrl("redirectedUrl?attribute=redirectWithRedirectView")); |
54 | | - } |
55 | | - |
56 | | - @Test |
57 | | - public void whenRedirectOnUrlWithUsingForwardPrefix_thenStatusOkAndForwardedOnUrl() throws Exception { |
58 | | - this.mockMvc.perform(get("/redirectWithForwardPrefix")).andExpect(status().isOk()).andExpect(view().name("forward:/redirectedUrl")).andExpect(model().attribute("attribute", is("redirectWithForwardPrefix"))).andExpect(forwardedUrl("/redirectedUrl")); |
59 | | - } |
| 29 | + private MockMvc mockMvc; |
| 30 | + |
| 31 | + @Autowired |
| 32 | + protected WebApplicationContext wac; |
| 33 | + |
| 34 | + @Before |
| 35 | + public void setup() { |
| 36 | + mockMvc = webAppContextSetup(wac).build(); |
| 37 | + } |
| 38 | + |
| 39 | + @Test |
| 40 | + public void whenRedirectOnUrlWithUsingXMLConfig_thenStatusRedirectionAndRedirectedOnUrl() throws Exception { |
| 41 | + mockMvc.perform(get("/redirectWithXMLConfig")).andExpect(status().is3xxRedirection()) |
| 42 | + .andExpect(view().name("RedirectedUrl")) |
| 43 | + .andExpect(model().attribute("attribute", is("redirectWithXMLConfig"))) |
| 44 | + .andExpect(redirectedUrl("redirectedUrl?attribute=redirectWithXMLConfig")); |
| 45 | + } |
| 46 | + |
| 47 | + @Test |
| 48 | + public void whenRedirectOnUrlWithUsingRedirectPrefix_thenStatusRedirectionAndRedirectedOnUrl() throws Exception { |
| 49 | + mockMvc.perform(get("/redirectWithRedirectPrefix")).andExpect(status().is3xxRedirection()).andExpect(view().name("redirect:/redirectedUrl")).andExpect(model().attribute("attribute", is("redirectWithRedirectPrefix"))) |
| 50 | + .andExpect(redirectedUrl("/redirectedUrl?attribute=redirectWithRedirectPrefix")); |
| 51 | + } |
| 52 | + |
| 53 | + @Test |
| 54 | + public void whenRedirectOnUrlWithUsingRedirectAttributes_thenStatusRedirectionAndRedirectedOnUrlAndAddedAttributeToFlashScope() throws Exception { |
| 55 | + mockMvc.perform(get("/redirectWithRedirectAttributes")).andExpect(status().is3xxRedirection()).andExpect(flash().attribute("flashAttribute", is("redirectWithRedirectAttributes"))) |
| 56 | + .andExpect(model().attribute("attribute", is("redirectWithRedirectAttributes"))) |
| 57 | + .andExpect(model().attribute("flashAttribute", is(nullValue()))) |
| 58 | + .andExpect(redirectedUrl("redirectedUrl?attribute=redirectWithRedirectAttributes")); |
| 59 | + } |
| 60 | + |
| 61 | + @Test |
| 62 | + public void whenRedirectOnUrlWithUsingRedirectView_thenStatusRedirectionAndRedirectedOnUrlAndAddedAttributeToFlashScope() throws Exception { |
| 63 | + mockMvc.perform(get("/redirectWithRedirectView")).andExpect(status().is3xxRedirection()).andExpect(model().attribute("attribute", is("redirectWithRedirectView"))).andExpect(redirectedUrl("redirectedUrl?attribute=redirectWithRedirectView")); |
| 64 | + } |
| 65 | + |
| 66 | + @Test |
| 67 | + public void whenRedirectOnUrlWithUsingForwardPrefix_thenStatusOkAndForwardedOnUrl() throws Exception { |
| 68 | + mockMvc.perform(get("/redirectWithForwardPrefix")).andExpect(status().isOk()).andExpect(view().name("forward:/redirectedUrl")).andExpect(model().attribute("attribute", is("redirectWithForwardPrefix"))).andExpect(forwardedUrl("/redirectedUrl")); |
| 69 | + } |
60 | 70 |
|
61 | 71 | } |
0 commit comments