We can also use @WithMockUser on top of your test to define the mocked user for the whole test execution:
xxxxxxxxxx
@Test
@WithMockUser("duke")
public void shouldRejectDeletingReviewsWhenUserLacksAdminRole() throws Exception {
this.mockMvc
.perform(delete("/api/tasks/42"))
.andExpect(status().isForbidden());
}
https://docs.spring.io/spring-security/site/docs/4.2.x/reference/html/test-method.html