This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class UserRepositoryImpl implements UserRepository { | |
@Override | |
public User findByUsername(String username) { | |
return new User(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public interface UserRepository { | |
User findByUsername(String username); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Test(expected = RuntimeException.class) | |
public void userLoginWhereUserIsNotFound() { | |
String username = "kentbeck"; | |
String password = "letmein"; | |
UserRepository userRepository = new UserRepositoryImpl(); | |
UserService userService = new UserService(userRepository); | |
User user = userService.authenticate(username, password); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class UserService { | |
private UserRepository userRepository; | |
public UserService(UserRepository userRepository) { | |
this.userRepository = userRepository; | |
} | |
public User authenticate(String username, String password) { | |
User foundUser = userRepository.findByUsername(username) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class UserService { | |
private UserRepository userRepository; | |
public UserService(UserRepository userRepository) { | |
this.userRepository = userRepository; | |
} | |
public User authenticate(String username, String password) { | |
User foundUser = null; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Test | |
public void userCanLogin() { | |
String username = "adrianvdh"; | |
String password = "hello123"; | |
UserRepository userRepository = new UserRepositoryImpl(); | |
UserService userService = new UserService(userRepository); | |
User user = userService.authenticate(username, password); | |
Assert.assertEquals("adrianvdh", user.username); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Test(expected = RuntimeException.class) | |
public void testFindByUsername_UsernameNotFound() { | |
String username = "kentb"; | |
UserRepository userRepository = new UserRepositoryImpl(); | |
//when | |
User user = userRepository.findByUsername(username); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class UserRepositoryImpl implements UserRepository { | |
Set<User> users = new HashSet<>(); | |
{ | |
User user = new User(); | |
user.username = "adrianvdh"; | |
user.password = "hello123"; | |
users.add(user); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Test | |
public void testFindByUsername() { | |
String username = "adrianvdh"; | |
UserRepository userRepository = new UserRepositoryImpl(); | |
User user = userRepository.findByUsername(username); | |
Assert.assertEquals("adrianvdh", user.username); | |
} |
NewerOlder