-
-
Save kikers25/c976b1d111805d0e212e to your computer and use it in GitHub Desktop.
Constructor Injection
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) { | |
User user; | |
user = userDAO.findUserBy(email); | |
if (user.isEmpty()) { | |
throw new RuntimeException("user doesn't have permissions"); | |
} | |
// do something | |
return user; | |
} | |
} |
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
UserService userService = new UserService(new UserDAOOracle()); | |
userService.signIn(email, password); |
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
@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