UserService implements UserDetailsService { @Override public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException { User user = userRepository.findByUsername(username); if(user == null) { throw new UsernameNotFoundException(username); } return new org.springframework.security.core.userdetails.User(user.getUsername(), user.getPassword(), Arrays.asList(new SimpleGrantedAuthority(user.getRole()))); } UserService(UserRepository userRepository); @Override UserDetails loadUserByUsername(String username); @Transactional(rollbackFor = Exception.class) User create(User user);  }
@Test public void getAllCommentsForToday_HappyPath_ShouldReturn1Comment() { User user = new User(); user.setUsername("shazin"); user.setPassword("sha908"); user.setRole("USER"); when(userRepository.findByUsername("shazin")).thenReturn(user); UserDetails actual = userService.loadUserByUsername("shazin"); verify(userRepository, times(1)).findByUsername("shazin"); }