I want to test the login process in a Spring Boot Application using MockMvc. After the successful login, the user gets 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 delivers the expected results.
In addition, I must test the HTTP status code of the redirected page (/home). Lets say the /home-page returns an HTTP 500 internal Server error, I need to be able to test 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 getting a 200 or a 500 (in case of an error), I get the status code 302.
Is there any way to correctly test the HTTP status code when following a redirect-URL?
Thanks and best regards