Skip to content

Instantly share code, notes, and snippets.

@kikers25
Last active August 29, 2015 14:01
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/c976b1d111805d0e212e to your computer and use it in GitHub Desktop.
Save kikers25/c976b1d111805d0e212e to your computer and use it in GitHub Desktop.
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