mockito説明用、試験対象クラス
public interface LoginRepository { | |
String findHashPassword(String loginId); | |
} |
public interface LoginService { | |
boolean login(String loginId, String password) throws AuthenticationException; | |
} |
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