I want to test the login process of a Spring boot application using MockMvc. After successful login, the user is redirected to / home. To test this, I use:
@Test
public void testLogin() throws Exception {
RequestBuilder requestBuilder = formLogin().user("test@tester.de").password("test");
mockMvc.perform(requestBuilder).andExpect(redirectedUrl("/home")).andExpect(status().isFound());
}
This test provides the expected results.
In addition, I have to check the HTTP status code of the redirected page (/ home). Assuming the / home page returns an internal HTTP 500 server error, I need to check this.
I tried the following:
@Test
public void testLogin() throws Exception {
RequestBuilder requestBuilder = formLogin().user("test@tester.de").password("test");
mockMvc.perform(requestBuilder).andExpect(redirectedUrl("/home")).andExpect(status().isFound());
mockMvc.perform(get("/home").with(csrf())).andExpect(status().isOk());
}
Instead, if you get 200 or 500 (in case of an error), I get a status code of 302.
Is there a way to correctly check the HTTP status code when using a redirect URL?
Thanks and best regards