Skip to content

Commit c4bc953

Browse files
author
alex-semenyuk
committed
Fix issues with tests.
1 parent 00dd88a commit c4bc953

5 files changed

Lines changed: 125 additions & 86 deletions

File tree

‎spring-rest/pom.xml‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,13 @@
153153
<version>${mockito.version}</version>
154154
<scope>test</scope>
155155
</dependency>
156+
157+
<dependency>
158+
<groupId>org.springframework</groupId>
159+
<artifactId>spring-test</artifactId>
160+
<version>${spring.version}</version>
161+
</dependency>
162+
156163

157164
</dependencies>
158165

@@ -172,8 +179,8 @@
172179
<artifactId>maven-compiler-plugin</artifactId>
173180
<version>${maven-compiler-plugin.version}</version>
174181
<configuration>
175-
<source>1.8</source>
176-
<target>1.8</target>
182+
<source>1.7</source>
183+
<target>1.7</target>
177184
</configuration>
178185
</plugin>
179186

Lines changed: 37 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package org.baeldung.redirect;
1+
package org.baeldung.web.controller.redirect;
22

33
import org.springframework.stereotype.Controller;
44
import org.springframework.ui.ModelMap;
@@ -13,40 +13,40 @@
1313
@RequestMapping("/")
1414
public class RedirectController {
1515

16-
@RequestMapping(value = "/redirectWithXMLConfig", method = RequestMethod.GET)
17-
public ModelAndView redirectWithUsingXMLConfig(final ModelMap model) {
18-
model.addAttribute("attribute", "redirectWithXMLConfig");
19-
return new ModelAndView("RedirectedUrl", model);
20-
}
21-
22-
@RequestMapping(value = "/redirectWithRedirectPrefix", method = RequestMethod.GET)
23-
public ModelAndView redirectWithUsingRedirectPrefix(final ModelMap model) {
24-
model.addAttribute("attribute", "redirectWithRedirectPrefix");
25-
return new ModelAndView("redirect:/redirectedUrl", model);
26-
}
27-
28-
@RequestMapping(value = "/redirectWithRedirectAttributes", method = RequestMethod.GET)
29-
public RedirectView redirectWithRedirectAttributes(final RedirectAttributes redirectAttributes) {
30-
redirectAttributes.addFlashAttribute("flashAttribute", "redirectWithRedirectAttributes");
31-
redirectAttributes.addAttribute("attribute", "redirectWithRedirectAttributes");
32-
return new RedirectView("redirectedUrl");
33-
}
34-
35-
@RequestMapping(value = "/redirectWithRedirectView", method = RequestMethod.GET)
36-
public RedirectView redirectWithUsingRedirectView(final ModelMap model) {
37-
model.addAttribute("attribute", "redirectWithRedirectView");
38-
return new RedirectView("redirectedUrl");
39-
}
40-
41-
@RequestMapping(value = "/redirectWithForwardPrefix", method = RequestMethod.GET)
42-
public ModelAndView redirectWithUsingForwardPrefix(final ModelMap model) {
43-
model.addAttribute("attribute", "redirectWithForwardPrefix");
44-
return new ModelAndView("forward:/redirectedUrl", model);
45-
}
46-
47-
@RequestMapping(value = "/redirectedUrl", method = RequestMethod.GET)
48-
public ModelAndView redirection(final ModelMap model, @ModelAttribute("flashAttribute") final Object flashAttribute) {
49-
model.addAttribute("redirectionAttribute", flashAttribute);
50-
return new ModelAndView("redirection", model);
51-
}
16+
@RequestMapping(value = "/redirectWithXMLConfig", method = RequestMethod.GET)
17+
public ModelAndView redirectWithUsingXMLConfig(final ModelMap model) {
18+
model.addAttribute("attribute", "redirectWithXMLConfig");
19+
return new ModelAndView("RedirectedUrl", model);
20+
}
21+
22+
@RequestMapping(value = "/redirectWithRedirectPrefix", method = RequestMethod.GET)
23+
public ModelAndView redirectWithUsingRedirectPrefix(final ModelMap model) {
24+
model.addAttribute("attribute", "redirectWithRedirectPrefix");
25+
return new ModelAndView("redirect:/redirectedUrl", model);
26+
}
27+
28+
@RequestMapping(value = "/redirectWithRedirectAttributes", method = RequestMethod.GET)
29+
public RedirectView redirectWithRedirectAttributes(final RedirectAttributes redirectAttributes) {
30+
redirectAttributes.addFlashAttribute("flashAttribute", "redirectWithRedirectAttributes");
31+
redirectAttributes.addAttribute("attribute", "redirectWithRedirectAttributes");
32+
return new RedirectView("redirectedUrl");
33+
}
34+
35+
@RequestMapping(value = "/redirectWithRedirectView", method = RequestMethod.GET)
36+
public RedirectView redirectWithUsingRedirectView(final ModelMap model) {
37+
model.addAttribute("attribute", "redirectWithRedirectView");
38+
return new RedirectView("redirectedUrl");
39+
}
40+
41+
@RequestMapping(value = "/redirectWithForwardPrefix", method = RequestMethod.GET)
42+
public ModelAndView redirectWithUsingForwardPrefix(final ModelMap model) {
43+
model.addAttribute("attribute", "redirectWithForwardPrefix");
44+
return new ModelAndView("forward:/redirectedUrl", model);
45+
}
46+
47+
@RequestMapping(value = "/redirectedUrl", method = RequestMethod.GET)
48+
public ModelAndView redirection(final ModelMap model, @ModelAttribute("flashAttribute") final Object flashAttribute) {
49+
model.addAttribute("redirectionAttribute", flashAttribute);
50+
return new ModelAndView("redirection", model);
51+
}
5252
}
Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,20 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<beans xmlns="http://www.springframework.org/schema/beans"
3-
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4-
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd" >
2+
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
53

6-
<bean id="multipartResolver"
4+
<context:component-scan base-package="org.baeldung.web" />
5+
6+
<mvc:annotation-driven />
7+
8+
<bean
9+
class="org.springframework.web.servlet.view.InternalResourceViewResolver" />
10+
<bean class="org.springframework.web.servlet.view.XmlViewResolver">
11+
<property name="location">
12+
<value>/WEB-INF/spring-views.xml</value>
13+
</property>
14+
<property name="order" value="0" />
15+
</bean>
16+
17+
<bean id="multipartResolver"
718
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
819
<!-- max upload size in bytes -->
920
<property name="maxUploadSize" value="20971520" /> <!-- 20MB -->
@@ -13,4 +24,5 @@
1324

1425
</bean>
1526

27+
1628
</beans>
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<beans xmlns="http://www.springframework.org/schema/beans"
2+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://www.springframework.org/schema/beans
4+
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
5+
6+
<bean id="RedirectedUrl" class="org.springframework.web.servlet.view.RedirectView">
7+
<property name="url" value="redirectedUrl" />
8+
</bean>
9+
10+
</beans>
Lines changed: 53 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
1-
package org.baeldung.redirect;
1+
package org.baeldung.web.controller.redirect;
22

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;
713

814
import org.junit.Before;
915
import org.junit.Test;
@@ -16,46 +22,50 @@
1622
import org.springframework.web.context.WebApplicationContext;
1723

1824
@RunWith(SpringJUnit4ClassRunner.class)
25+
@ContextConfiguration("file:src/main/webapp/WEB-INF/api-servlet.xml")
1926
@WebAppConfiguration
20-
@ContextConfiguration("file:src/main/webapp/WEB-INF/mvc-dispatcher-servlet.xml")
2127
public class RedirectControllerTest {
2228

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+
}
6070

6171
}

0 commit comments

Comments
 (0)