4

I'm using Spring 3.2.11.RELEASE and JUnit 4.11. In a particular Spring controller, I have a method that ends thusly ...

return new ModelAndView(new RedirectView(redirectUri, true));

In my JUnit test, how do I verify return from a submission to my controller in which this RedirectView is returned? I used to use org.springframework.test.web.AbstractModelAndViewTests.assertViewName, but that only returns "null", even when a non-empty ModelAndView object is returned. Here is how I'm constructing 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 verify that a RedirectView comes back with the proper value is appreciatd,

Dave
  • 15,639
  • 133
  • 442
  • 830

2 Answers2

10

As Koiter said, consider moving to spring-test a and MockMvc

It providers some methods to test controllers and requests/reponses in a declarative way

you will need a @Autowired WebApplicationContext wac;

and on your @Before method setup this to use the @WebAppConfiguration of the class.

You'll 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 the MockMvcResultMatchers to assert things

 @Test
  public void testMyRedirect() throws Exception {
   mockMvc.perform(post("you/url/")
    .andExpect(status().isOk())
    .andExpect(redirectUrl("you/redirect")
}

Note: post(), status() isOk() redirectUrl() are statics imports from MockMvcResultMatchers

See more what you can match here

Joao Evangelista
  • 2,407
  • 2
  • 26
  • 37
0

Considering change your tool to MockMvc.

First you should create your MockMvc based on your controller.

private MockMvc mockController;

    mockController =
            MockMvcBuilders.standaloneSetup(loginController).setCustomArgumentResolvers(
                    new ServletWebArgumentResolverAdapter(new PageableArgumentResolver())).build();

After you create that object build the request with the request information. Part of this is the assert options 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));

The MockMvcResultMatchers contains a method for reviewing redirect information.

MockMvc from spring is a good choice to apply your unit testing on the controller layer.

Koitoer
  • 18,778
  • 7
  • 63
  • 86