From c4bc953d1c1288c6bf634bdecc5b0cd2587e1742 Mon Sep 17 00:00:00 2001 From: alex-semenyuk Date: Tue, 21 Jul 2015 13:06:14 +0200 Subject: [PATCH] Fix issues with tests. --- spring-rest/pom.xml | 11 ++- .../redirect/RedirectController.java | 64 +++++++-------- .../src/main/webapp/WEB-INF/api-servlet.xml | 20 ++++- .../src/main/webapp/WEB-INF/spring-views.xml | 10 +++ .../redirect/RedirectControllerTest.java | 82 +++++++++++-------- 5 files changed, 113 insertions(+), 74 deletions(-) create mode 100644 spring-rest/src/main/webapp/WEB-INF/spring-views.xml diff --git a/spring-rest/pom.xml b/spring-rest/pom.xml index 542fa1d025..ecfe1f40f0 100644 --- a/spring-rest/pom.xml +++ b/spring-rest/pom.xml @@ -153,6 +153,13 @@ ${mockito.version} test + + + org.springframework + spring-test + ${spring.version} + + @@ -172,8 +179,8 @@ maven-compiler-plugin ${maven-compiler-plugin.version} - 1.8 - 1.8 + 1.7 + 1.7 diff --git a/spring-rest/src/main/java/org/baeldung/web/controller/redirect/RedirectController.java b/spring-rest/src/main/java/org/baeldung/web/controller/redirect/RedirectController.java index e687f76e62..db83495c6f 100644 --- a/spring-rest/src/main/java/org/baeldung/web/controller/redirect/RedirectController.java +++ b/spring-rest/src/main/java/org/baeldung/web/controller/redirect/RedirectController.java @@ -1,4 +1,4 @@ -package org.baeldung.redirect; +package org.baeldung.web.controller.redirect; import org.springframework.stereotype.Controller; import org.springframework.ui.ModelMap; @@ -13,40 +13,40 @@ import org.springframework.web.servlet.view.RedirectView; @RequestMapping("/") public class RedirectController { - @RequestMapping(value = "/redirectWithXMLConfig", method = RequestMethod.GET) - public ModelAndView redirectWithUsingXMLConfig(final ModelMap model) { - model.addAttribute("attribute", "redirectWithXMLConfig"); - return new ModelAndView("RedirectedUrl", model); - } + @RequestMapping(value = "/redirectWithXMLConfig", method = RequestMethod.GET) + public ModelAndView redirectWithUsingXMLConfig(final ModelMap model) { + model.addAttribute("attribute", "redirectWithXMLConfig"); + return new ModelAndView("RedirectedUrl", model); + } - @RequestMapping(value = "/redirectWithRedirectPrefix", method = RequestMethod.GET) - public ModelAndView redirectWithUsingRedirectPrefix(final ModelMap model) { - model.addAttribute("attribute", "redirectWithRedirectPrefix"); - return new ModelAndView("redirect:/redirectedUrl", model); - } + @RequestMapping(value = "/redirectWithRedirectPrefix", method = RequestMethod.GET) + public ModelAndView redirectWithUsingRedirectPrefix(final ModelMap model) { + model.addAttribute("attribute", "redirectWithRedirectPrefix"); + return new ModelAndView("redirect:/redirectedUrl", model); + } - @RequestMapping(value = "/redirectWithRedirectAttributes", method = RequestMethod.GET) - public RedirectView redirectWithRedirectAttributes(final RedirectAttributes redirectAttributes) { - redirectAttributes.addFlashAttribute("flashAttribute", "redirectWithRedirectAttributes"); - redirectAttributes.addAttribute("attribute", "redirectWithRedirectAttributes"); - return new RedirectView("redirectedUrl"); - } + @RequestMapping(value = "/redirectWithRedirectAttributes", method = RequestMethod.GET) + public RedirectView redirectWithRedirectAttributes(final RedirectAttributes redirectAttributes) { + redirectAttributes.addFlashAttribute("flashAttribute", "redirectWithRedirectAttributes"); + redirectAttributes.addAttribute("attribute", "redirectWithRedirectAttributes"); + return new RedirectView("redirectedUrl"); + } - @RequestMapping(value = "/redirectWithRedirectView", method = RequestMethod.GET) - public RedirectView redirectWithUsingRedirectView(final ModelMap model) { - model.addAttribute("attribute", "redirectWithRedirectView"); - return new RedirectView("redirectedUrl"); - } + @RequestMapping(value = "/redirectWithRedirectView", method = RequestMethod.GET) + public RedirectView redirectWithUsingRedirectView(final ModelMap model) { + model.addAttribute("attribute", "redirectWithRedirectView"); + return new RedirectView("redirectedUrl"); + } - @RequestMapping(value = "/redirectWithForwardPrefix", method = RequestMethod.GET) - public ModelAndView redirectWithUsingForwardPrefix(final ModelMap model) { - model.addAttribute("attribute", "redirectWithForwardPrefix"); - return new ModelAndView("forward:/redirectedUrl", model); - } + @RequestMapping(value = "/redirectWithForwardPrefix", method = RequestMethod.GET) + public ModelAndView redirectWithUsingForwardPrefix(final ModelMap model) { + model.addAttribute("attribute", "redirectWithForwardPrefix"); + return new ModelAndView("forward:/redirectedUrl", model); + } - @RequestMapping(value = "/redirectedUrl", method = RequestMethod.GET) - public ModelAndView redirection(final ModelMap model, @ModelAttribute("flashAttribute") final Object flashAttribute) { - model.addAttribute("redirectionAttribute", flashAttribute); - return new ModelAndView("redirection", model); - } + @RequestMapping(value = "/redirectedUrl", method = RequestMethod.GET) + public ModelAndView redirection(final ModelMap model, @ModelAttribute("flashAttribute") final Object flashAttribute) { + model.addAttribute("redirectionAttribute", flashAttribute); + return new ModelAndView("redirection", model); + } } \ No newline at end of file diff --git a/spring-rest/src/main/webapp/WEB-INF/api-servlet.xml b/spring-rest/src/main/webapp/WEB-INF/api-servlet.xml index acab871c88..2e01bb636b 100644 --- a/spring-rest/src/main/webapp/WEB-INF/api-servlet.xml +++ b/spring-rest/src/main/webapp/WEB-INF/api-servlet.xml @@ -1,9 +1,20 @@ - + - + + + + + + + /WEB-INF/spring-views.xml + + + + + @@ -13,4 +24,5 @@ + \ No newline at end of file diff --git a/spring-rest/src/main/webapp/WEB-INF/spring-views.xml b/spring-rest/src/main/webapp/WEB-INF/spring-views.xml new file mode 100644 index 0000000000..83c7828d9a --- /dev/null +++ b/spring-rest/src/main/webapp/WEB-INF/spring-views.xml @@ -0,0 +1,10 @@ + + + + + + + \ No newline at end of file diff --git a/spring-rest/src/test/java/org/baeldung/web/controller/redirect/RedirectControllerTest.java b/spring-rest/src/test/java/org/baeldung/web/controller/redirect/RedirectControllerTest.java index 9b16891336..12ac394f90 100644 --- a/spring-rest/src/test/java/org/baeldung/web/controller/redirect/RedirectControllerTest.java +++ b/spring-rest/src/test/java/org/baeldung/web/controller/redirect/RedirectControllerTest.java @@ -1,9 +1,15 @@ -package org.baeldung.redirect; +package org.baeldung.web.controller.redirect; -import static org.hamcrest.CoreMatchers.*; -import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; -import static org.springframework.test.web.servlet.setup.MockMvcBuilders.*; +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.CoreMatchers.nullValue; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.flash; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.forwardedUrl; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.model; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.redirectedUrl; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.view; +import static org.springframework.test.web.servlet.setup.MockMvcBuilders.webAppContextSetup; import org.junit.Before; import org.junit.Test; @@ -16,46 +22,50 @@ import org.springframework.test.web.servlet.MockMvc; import org.springframework.web.context.WebApplicationContext; @RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration("file:src/main/webapp/WEB-INF/api-servlet.xml") @WebAppConfiguration -@ContextConfiguration("file:src/main/webapp/WEB-INF/mvc-dispatcher-servlet.xml") public class RedirectControllerTest { - private MockMvc mockMvc; + private MockMvc mockMvc; - @Autowired - protected WebApplicationContext wac; + @Autowired + protected WebApplicationContext wac; - @Before - public void setup() { - this.mockMvc = webAppContextSetup(this.wac).build(); - } + @Before + public void setup() { + mockMvc = webAppContextSetup(wac).build(); + } - @Test - public void whenRedirectOnUrlWithUsingXMLConfig_thenStatusRedirectionAndRedirectedOnUrl() throws Exception { - this.mockMvc.perform(get("/redirectWithXMLConfig")).andExpect(status().is3xxRedirection()).andExpect(view().name("RedirectedUrl")).andExpect(model().attribute("attribute", is("redirectWithXMLConfig"))) - .andExpect(redirectedUrl("redirectedUrl?attribute=redirectWithXMLConfig")); - } + @Test + public void whenRedirectOnUrlWithUsingXMLConfig_thenStatusRedirectionAndRedirectedOnUrl() throws Exception { + mockMvc.perform(get("/redirectWithXMLConfig")).andExpect(status().is3xxRedirection()) + .andExpect(view().name("RedirectedUrl")) + .andExpect(model().attribute("attribute", is("redirectWithXMLConfig"))) + .andExpect(redirectedUrl("redirectedUrl?attribute=redirectWithXMLConfig")); + } - @Test - public void whenRedirectOnUrlWithUsingRedirectPrefix_thenStatusRedirectionAndRedirectedOnUrl() throws Exception { - this.mockMvc.perform(get("/redirectWithRedirectPrefix")).andExpect(status().is3xxRedirection()).andExpect(view().name("redirect:/redirectedUrl")).andExpect(model().attribute("attribute", is("redirectWithRedirectPrefix"))) - .andExpect(redirectedUrl("/redirectedUrl?attribute=redirectWithRedirectPrefix")); - } + @Test + public void whenRedirectOnUrlWithUsingRedirectPrefix_thenStatusRedirectionAndRedirectedOnUrl() throws Exception { + mockMvc.perform(get("/redirectWithRedirectPrefix")).andExpect(status().is3xxRedirection()).andExpect(view().name("redirect:/redirectedUrl")).andExpect(model().attribute("attribute", is("redirectWithRedirectPrefix"))) + .andExpect(redirectedUrl("/redirectedUrl?attribute=redirectWithRedirectPrefix")); + } - @Test - public void whenRedirectOnUrlWithUsingRedirectAttributes_thenStatusRedirectionAndRedirectedOnUrlAndAddedAttributeToFlashScope() throws Exception { - this.mockMvc.perform(get("/redirectWithRedirectAttributes")).andExpect(status().is3xxRedirection()).andExpect(flash().attribute("flashAttribute", is("redirectWithRedirectAttributes"))) - .andExpect(model().attribute("attribute", is("redirectWithRedirectAttributes"))).andExpect(model().attribute("flashAttribute", is(nullValue()))).andExpect(redirectedUrl("redirectedUrl?attribute=redirectWithRedirectAttributes")); - } + @Test + public void whenRedirectOnUrlWithUsingRedirectAttributes_thenStatusRedirectionAndRedirectedOnUrlAndAddedAttributeToFlashScope() throws Exception { + mockMvc.perform(get("/redirectWithRedirectAttributes")).andExpect(status().is3xxRedirection()).andExpect(flash().attribute("flashAttribute", is("redirectWithRedirectAttributes"))) + .andExpect(model().attribute("attribute", is("redirectWithRedirectAttributes"))) + .andExpect(model().attribute("flashAttribute", is(nullValue()))) + .andExpect(redirectedUrl("redirectedUrl?attribute=redirectWithRedirectAttributes")); + } - @Test - public void whenRedirectOnUrlWithUsingRedirectView_thenStatusRedirectionAndRedirectedOnUrlAndAddedAttributeToFlashScope() throws Exception { - this.mockMvc.perform(get("/redirectWithRedirectView")).andExpect(status().is3xxRedirection()).andExpect(model().attribute("attribute", is("redirectWithRedirectView"))).andExpect(redirectedUrl("redirectedUrl?attribute=redirectWithRedirectView")); - } + @Test + public void whenRedirectOnUrlWithUsingRedirectView_thenStatusRedirectionAndRedirectedOnUrlAndAddedAttributeToFlashScope() throws Exception { + mockMvc.perform(get("/redirectWithRedirectView")).andExpect(status().is3xxRedirection()).andExpect(model().attribute("attribute", is("redirectWithRedirectView"))).andExpect(redirectedUrl("redirectedUrl?attribute=redirectWithRedirectView")); + } - @Test - public void whenRedirectOnUrlWithUsingForwardPrefix_thenStatusOkAndForwardedOnUrl() throws Exception { - this.mockMvc.perform(get("/redirectWithForwardPrefix")).andExpect(status().isOk()).andExpect(view().name("forward:/redirectedUrl")).andExpect(model().attribute("attribute", is("redirectWithForwardPrefix"))).andExpect(forwardedUrl("/redirectedUrl")); - } + @Test + public void whenRedirectOnUrlWithUsingForwardPrefix_thenStatusOkAndForwardedOnUrl() throws Exception { + mockMvc.perform(get("/redirectWithForwardPrefix")).andExpect(status().isOk()).andExpect(view().name("forward:/redirectedUrl")).andExpect(model().attribute("attribute", is("redirectWithForwardPrefix"))).andExpect(forwardedUrl("/redirectedUrl")); + } }