-
-
Save kikers25/51a77fb23a831de17fce to your computer and use it in GitHub Desktop.
Stubs v2
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 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(); | |
} | |
} |
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 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