Code example for MockMvc

Methods: perform

0
    } 
 
    @Test
    public void requestProtectedUrlWithAdmin() throws Exception {
        mvc
            .perform(get("/admin").with(user("admin").roles("ADMIN")))
            // Ensure we got past Security 
            .andExpect(status().isNotFound())
            // Ensure it appears we are authenticated with admin 
            .andExpect(authenticated().withUsername("admin")); 
    } 
 
    @Test
    public void requestProtectedUrlWithUserDetails() throws Exception {
        UserDetails user = userDetailsService.loadUserByUsername("user");
        mvc
            .perform(get("/").with(user(user)))
            // Ensure we got past Security 
            .andExpect(status().isNotFound())
            // Ensure it appears we are authenticated with user 
Contextual code suggestions in your IDE  Get Codota for Java