JwtUserDetailsService implements UserDetailsService { @Override public UserDetails loadUserByUsername(String username) { final User user = userRepository.findByUsername(username); if (user == null) { throw new UsernameNotFoundException( String.format("No user found with username '%s'.", username)); } else { return JwtUserFactory.create(user); } } @Autowired  JwtUserDetailsService(UserRepository userRepository); @Override UserDetails loadUserByUsername(String username);  }
@Test public void whenLoadByUsernameCalledWithKnownUsernameThenExpectUserDetailsToBeReturned() { PowerMock.mockStatic(JwtUserFactory.class); final User user = PowerMock.createMock(User.class); final JwtUser jwtUser = EasyMock.createMock(JwtUser.class); expect(userRepository.findByUsername(KNOWN_USERNAME)).andStubReturn(user); expect(JwtUserFactory.create(eq(user))).andStubReturn(jwtUser); PowerMock.replayAll(); final JwtUserDetailsService jwtUserDetailsService = new JwtUserDetailsService(userRepository); final JwtUser userDetails = (JwtUser) jwtUserDetailsService.loadUserByUsername(KNOWN_USERNAME); assertEquals(jwtUser, userDetails); PowerMock.verifyAll(); }
@Test(expected = UsernameNotFoundException.class) public void whenLoadByUsernameCalledWithUnknownUsernameThenExpectUsernameNotFoundException() { expect(userRepository.findByUsername(UNKNOWN_USERNAME)).andStubReturn(null); PowerMock.replayAll(); final JwtUserDetailsService jwtUserDetailsService = new JwtUserDetailsService(userRepository); jwtUserDetailsService.loadUserByUsername(UNKNOWN_USERNAME); PowerMock.verifyAll(); }