I have started to use @PreAuthorize
, which i want to test using testNg
.
I am using withUserContext("username")
; But every test I run gets passed but in real time the authorisation is done perfectly in the portal.
Can someone help me out of this.
@Test
private void testFunctionWith@PreAuthorize() throws Exception {
withUserContext("username");
}
public SecurityContext withUserContext(String username) {
UserDetails principal = asyncCall(username);
Authentication authentication = new UsernamePasswordAuthenticationToken(principal, principal.getPassword(),
principal.getAuthorities());
SecurityContext context = SecurityContextHolder.getContext();
context.setAuthentication(authentication);
return context;
}
@Async
public UserDetails asyncCall(String username) {
return userDetailsService.loadUserByUsername(username);
}
The tests that are expected to fail are getting passed.