Last active
September 30, 2016 14:41
-
-
Save kuneo/f94e0da8ff723f419978c756882e3ca9 to your computer and use it in GitHub Desktop.
mockito説明用、試験対象クラス
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 interface LoginRepository { | |
String findHashPassword(String loginId); | |
} |
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 interface LoginService { | |
boolean login(String loginId, String password) throws AuthenticationException; | |
} |
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 LoginServiceImpl implements LoginService { | |
/** | |
* Serviceから呼び出す下位リポジトリ | |
* 試験クラスからモック化する | |
*/ | |
@Inject | |
LoginRepository loginRepository; | |
@Override | |
public boolean login(String loginId, String password) throws AuthenticationException { | |
String hashPassword = loginRepository.findHashPassword(loginId); | |
if (hashPassword == null) { | |
throw new AuthenticationException(); | |
} | |
return check(password, hashPassword); | |
} | |
/** | |
* パスワード平文をmd5に変換してDBから取得した値と比較する | |
*/ | |
private boolean check(String password, String hashPassword) { | |
String hash = DigestUtils.md5Hex(password); | |
return hashPassword.equals(hash); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment