Test HTTP status code of redirected URL with MockMvc

Ben picture Ben · Dec 14, 2016 · Viewed 15.2k times · Source

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("[email protected]").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("[email protected]").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

Answer

Nathan Russell picture Nathan Russell · Dec 14, 2016

First, I would split your test into 2 separate tests, because you are testing 2 quite different scenarios:

@Test
public void testSuccessfulLogin() throws Exception {
    RequestBuilder requestBuilder = formLogin().user("[email protected]").password("test");
    mockMvc.perform(requestBuilder).andExpect(redirectedUrl("/home")).andExpect(status().isFound());
}

@Test
public void testHomepageThrows500() throws Exception {

    // configure a mock service in the controller to throw an exception

    RequestBuilder requestBuilder = formLogin().user("[email protected]").password("test");
    mockMvc.perform(requestBuilder).andExpect(redirectedUrl("/home")).andExpect(status().is5xxServerError());
}

Your first test is that of the successful login scenario.

The second test, as you've worded it in your question, is that where the home page (assuming a controller) returns a HTTP 500.
To get to the home page, you still need to login - it's not the act of logging on that generates the error, its the controller itself once you've logged on.
To make the controller return a HTTP 500 you are going to need to simulate some error. Without seeing your controller, I can only guess there is some service that is injected in. In your test you should be able to provide a mock of that, and then configure the mock to throw an exception.

You should be able to inject a mock something like this:

@RunWith(SpringJUnit4ClassRunner.class)
@WebMvcTest(HomeController.class)
public class HomeControllerIntegrationTest {

    @Autowired
    private MockMvc mockMvc;

    @MockBean
    private YourService yourService;

And then within your test do something like the following (I'm using the BDD methods of mockito):

@Test
public void testHomepageThrows500() throws Exception {

    given(yourService.someMethod()).willThrow(new Exception("something bad happened");

    RequestBuilder requestBuilder = formLogin().user("[email protected]").password("test");
    mockMvc.perform(requestBuilder).andExpect(redirectedUrl("/home")).andExpect(status().is5xxServerError());
}