vendredi 22 janvier 2016

How do I mock the user in a Spring controller, and debug when something's missing or not mocked?

I have Spring MVC application (4.2.1) with a controller that requires "ADMIN" privileges. I'm trying to test that normal users get a "403", and admins get a "200". It works in the browser, but in my unit tests, both tests fail with a "302" status returned.

I believe that I've set everything up by the book to use @WithMockUser. I'd like to know how to figure out WHY I'm getting a 302 status. I suspect it's redirecting to the login page because it's not showing the user as logged in at all, but how do I verify that?

@Test
@WithMockUser(roles={"USER","ADMIN"})
public void requestProtectedUrlWithAdminRole() throws Exception {
    mvc.perform(get("/admin/").with(csrf()))
        .andExpect(status().isOk())
        .andExpect(content().string(containsString("Admin")))
        .andReturn();
}

Aucun commentaire:

Enregistrer un commentaire