Skip to content

Instantly share code, notes, and snippets.

@kikers25
Last active August 29, 2015 14:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kikers25/51a77fb23a831de17fce to your computer and use it in GitHub Desktop.
Save kikers25/51a77fb23a831de17fce to your computer and use it in GitHub Desktop.
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