0

Need to test the redirect in action response using mockmvc in Junit

Controller.java

@ActionMapping(param="submit")
public void handleSubmit(ActionRequest req, ActionResponse res) {
  ...
  res.sendRedirect("https://google.co.in"):
}

ControllerTest.java

  import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
  import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.redirectedUrl;
  import static org.springframework.test.web.servlet.setup.MockMvcBuilders.standaloneSetup;

public ControllerTest {
    private MockMvc mockmvc;

    @Before
    public void setup() {
      controller = new Controller();
      mockmvc = standaloneSetup(controller).build();
    }

    @Test
    public void redirectTest() throws Exception {
       mockMvc.perform(post("/action").param("submit", "value")).
                    andExpect(redirectedUrl("https://google.co.in"));
    }
}

The problem is, the controller method is not called on post perform from Junit.

The url in browser on click of submit looks as action/rparam=action=submit.wsp

Stefan Birkner
  • 24,059
  • 12
  • 57
  • 72
AJJ
  • 3,570
  • 7
  • 43
  • 76

0 Answers0