Skip to content

Instantly share code, notes, and snippets.

@kuneo
Last active September 30, 2016 14:41
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 kuneo/f94e0da8ff723f419978c756882e3ca9 to your computer and use it in GitHub Desktop.
Save kuneo/f94e0da8ff723f419978c756882e3ca9 to your computer and use it in GitHub Desktop.
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