Skip to content

Instantly share code, notes, and snippets.

View Adrianvdh's full-sized avatar

Adrian van den Houten Adrianvdh

  • Cape Town
View GitHub Profile
public class AuthorisationContext {
public void authenticate(String username, String password) {
}
}
public class UserRepositoryImpl implements UserRepository {
@Override
public User findByUsername(String username) {
return new User();
}
}
public interface UserRepository {
User findByUsername(String username);
}
@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);
}
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)
public class UserService {
private UserRepository userRepository;
public UserService(UserRepository userRepository) {
this.userRepository = userRepository;
}
public User authenticate(String username, String password) {
User foundUser = null;
@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);
@Test(expected = RuntimeException.class)
public void testFindByUsername_UsernameNotFound() {
String username = "kentb";
UserRepository userRepository = new UserRepositoryImpl();
//when
User user = userRepository.findByUsername(username);
}
public class UserRepositoryImpl implements UserRepository {
Set<User> users = new HashSet<>();
{
User user = new User();
user.username = "adrianvdh";
user.password = "hello123";
users.add(user);
}
@Test
public void testFindByUsername() {
String username = "adrianvdh";
UserRepository userRepository = new UserRepositoryImpl();
User user = userRepository.findByUsername(username);
Assert.assertEquals("adrianvdh", user.username);
}