Create a gist now

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Stubs v2
public class TestUserServiceV2 {
private static final Email EMAIL = new Email("user@company.com");
private final class StubUserDAOv2 implements UserDAO {
private User user;
public User findUserBy(Email email) {
return user;
}
public void setUserFoundByEmail(User user) {
this.user = user;
}
}
private StubUserDAOv2 stubUserDAO;
private UserService userService;
@Before
public void initialization() {
stubUserDAO = new StubUserDAOv2();
userService = new UserService(stubUserDAO);
}
@Test
public void //
should_throw_exception_when_passwords_are_different_version1() throws Exception {
stubUserDAO.setUserFoundByEmail(userWithPassword("password"));
try {
userService.signIn(EMAIL, new Password("password"));
fail("No threw exception");
} catch (AuthenticationException e) {
// ok
}
}
@Test(expected = AuthenticationException.class)
public void //
should_throw_exception_when_passwords_are_different_version2() throws Exception {
stubUserDAO.setUserFoundByEmail(userWithPassword("another_password"));
userService.signIn(EMAIL, new Password("password"));
}
private User userWithPassword(String password) {
return aUser() //
.password(password) //
.build();
}
}
public class UserService {
private UserDAO userDAO;
public UserService(UserDAO userDAO) {
this.userDAO = userDAO;
}
public User signIn(Email email, Password password) throws AuthenticationException {
User user;
user = userDAO.findUserBy(email);
// validation user
if (!password.is(user.getPassword())) {
throw new AuthenticationException("user has no permission");
}
// do something
return user;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment