I am going to test my controller using springsecurity.
@PostMapping("/xx")
public String xx(Authentication auth){
String userId = (String)auth.getPrincipal();
....
}
I have no idea how to mock Authentication object. Or is there other way to deal with it?
My test shown below
public class MyControllerTest{
@Autowired
private XxController xxController;
private MockMvc mockMvc;
@Before
public void setUp() {
mockMvc = MockMvcBuilders.standaloneSetup(xxController).build();
}
@Test
public void xxtestMethod() throws Exception {
MvcResult mvcResult = mockMvc.perform(MockMvcRequestBuilders.post(/xx))
.andExpect(MockMvcResultMatchers.status().isOk())
.andDo(MockMvcResultHandlers.print())
.andReturn();
}
}