需要使用Junit中的mockmvc测试动作响应中的重定向
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"));
}
}
问题是,在Junit的后期执行中没有调用控制器方法。
点击提交时浏览器中的网址显示为action/rparam=action=submit.wsp