In my JUnit test, how can I test Spring RedirectView?
I am using Spring 3.2.11.RELEASE and JUnit 4.11. In a specific Spring controller, I have a method that ends up like this ...
return new ModelAndView(new RedirectView(redirectUri, true));
In my JUnit test, how can I check the return from the view to my controller that this RedirectView is returning to? I used org.springframework.test.web.AbstractModelAndViewTests.assertViewName but this only returns "null" even if a non-empty ModelAndView object is returned. This is how I am building my JUnit test ...
request.setRequestURI("/mypage/launch");
request.setMethod("POST");
โฆ
final Object handler = handlerMapping.getHandler(request).getHandler();
final ModelAndView mav = handlerAdapter.handle(request, response, handler);
assertViewName(mav, "redirect:/landing");
Any help on how to check that the RedirectView is returning with the correct value is considered
source to share
As Koiter said, consider switching to spring-test a and MockMvc
It provides some methods for validating controllers and requests / reponses in a declarative way.
you will need @Autowired WebApplicationContext wac;
and in your method, @Before
set this to use @WebAppConfiguration
for the class.
You end up with something
@ContextConfiguration("youconfighere.xml")
//or (classes = {YourClassConfig.class}
@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
public class MyControllerTests {
@Autowired WebApplicationContext wac
private MockMvc mockMvc;
@Before
public void setup() {
//setup the mock to use the web context
this.mockMvc = MockMvcBuilders.webAppContextSetup(wac).build();
}
}
Then you just need to use MockMvcResultMatchers to confirm things
@Test
public void testMyRedirect() throws Exception {
mockMvc.perform(post("you/url/")
.andExpect(status().isOk())
.andExpect(redirectUrl("you/redirect")
}
Note: post(), status() isOk() redirectUrl()
- this is import statistics fromMockMvcResultMatchers
More details on what you can match here
source to share
Considering changing your tool to MockMvc.
First you have to create your MockMvc based on your controller.
private MockMvc mockController;
mockController =
MockMvcBuilders.standaloneSetup(loginController).setCustomArgumentResolvers(
new ServletWebArgumentResolverAdapter(new PageableArgumentResolver())).build();
After creating this object, create a request with the request information. Some of this is the assert parameters that are contained in the API.
mockController.perform(MockMvcRequestBuilders.get(LoginControllerTest.LOGIN_CONTROLLER_URL + "?logout=true").
principal(SessionProvider.getPrincipal("GonLu004")))
.andDo(MockMvcResultHandlers.print())
.andExpect(MockMvcResultMatchers.status().isOk())
.andExpect(MockMvcResultMatchers.view().name("jsp/login"))
.andExpect(MockMvcResultMatchers.model().attribute("logOutMessage", logoutMessage));
MockMvcResultMatchers contains a method to view redirection information .
Spring's MockMvc is a good choice for applying controller-level testing to your device.
source to share