Create a gist now

Instantly share code, notes, and snippets.

@kikers25 /1_UserService.java Secret
Last active Aug 29, 2015

Embed
What would you like to do?
Constructor Injection
public class UserService {
private UserDAO userDAO;
public UserService(UserDAO userDAO) {
this.userDAO = userDAO;
}
public User signIn(Email email, Password password) {
User user;
user = userDAO.findUserBy(email);
if (user.isEmpty()) {
throw new RuntimeException("user doesn't have permissions");
}
// do something
return user;
}
}
UserService userService = new UserService(new UserDAOOracle());
userService.signIn(email, password);
@Test(expected = RuntimeException.class)
public void should_throw_exception_when_user_wasnt_found() throws Exception {
FakeUserDAO fakeUserDAO = new FakeUserDAO();
UserService userService = new UserService(fakeUserDAO);
userService.signIn(email, password);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment