i want to test my REST Endpoints. In my REST methods are some filters based on the SecurityContext. When i set the SecurityContext in my test classes and call the rest methods, the SecurityContext is deleted. Only when i create the SecurityContext in my REST methods it has an effect. Is there a possibility how to transfer the context?
SecurityContext securityContext = new SecurityContextImpl();
securityContext.setAuthentication(authentication);
SecurityContextHolder.setContext(securityContext);
// is here User ...
.mockMvc.perform(get("/test"...)
RestController:
public ResponseEntity<...> getTest(){
// is now anonymus
SecurityContextHolder.getContext().getAuthentication().getPrincipal();
}
thank you