我想使用MockMvc在Spring Boot應用程序中測試登錄過程。成功登錄後,用戶將被重定向到/ home。爲了測試這個,我使用:使用MockMvc測試重定向URL的HTTP狀態代碼
@Test
public void testLogin() throws Exception {
RequestBuilder requestBuilder = formLogin().user("[email protected]").password("test");
mockMvc.perform(requestBuilder).andExpect(redirectedUrl("/home")).andExpect(status().isFound());
}
這個測試提供了預期的結果。
另外,我必須測試重定向頁面(/ home)的HTTP狀態碼。假設/ home-page返回一個HTTP 500內部服務器錯誤,我需要能夠測試這個。
我試過如下:
@Test
public void testLogin() throws Exception {
RequestBuilder requestBuilder = formLogin().user("[email protected]").password("test");
mockMvc.perform(requestBuilder).andExpect(redirectedUrl("/home")).andExpect(status().isFound());
mockMvc.perform(get("/home").with(csrf())).andExpect(status().isOk());
}
相反,如果得到一個200或500(在發生錯誤的情況下),我得到的狀態碼302
有什麼方法來正確測試遵循重定向URL時的HTTP狀態代碼?
感謝和問候