我想使用spring mvc security登录以测试proj,但是在运行junit测试方法时引发了异常
测试方法:
@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)
答案 0 :(得分:0)
状态代码为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();
会奏效。