Junit测试spring mvc
我想使用SpringMVC安全性登录以测试proj,但在运行junit测试方法时引发了异常 试验方法:Junit测试spring mvc,junit,spring-3,Junit,Spring 3,我想使用SpringMVC安全性登录以测试proj,但在运行junit测试方法时引发了异常 试验方法: @Test public void loginUser1Ok() throws Exception { HttpSession session = mockMvc .perform( post("/j_spring_security_check").param("j_username",
@Test
public void loginUser1Ok() throws Exception {
HttpSession session = mockMvc
.perform(
post("/j_spring_security_check").param("j_username",
"admin").param("j_password",
"secret"))
.andExpect(status().is(HttpStatus.FOUND.value()))
.andExpect(redirectedUrl("/")).andReturn().getRequest()
.getSession();
Assert.assertNotNull(session);
}
提出了这一例外
java.lang.AssertionError: Response status expected:<302> but was:<200>
at org.springframework.test.util.AssertionErrors.fail(AssertionErrors.java:60)
at org.springframework.test.util.AssertionErrors.assertEquals(AssertionErrors.java:89)
at org.springframework.test.web.servlet.result.StatusResultMatchers$2.match(StatusResultMatchers.java:61)
at org.springframework.test.web.servlet.MockMvc$1.andExpect(MockMvc.java:141)
at com.orange.weather.WebApplicationContextControllerTest.loginUser1Ok(WebApplicationContextControllerTest.java:157)
状态代码为200的响应不是重定向。我想
HttpSession session = mockMvc
.perform(
post("/j_spring_security_check").param("j_username",
"admin").param("j_password",
"secret"))
.andExpect(status().is(HttpStatus.FOUND.value()))
.getSession();
会有用的