2

我如何在 jmockit class A { private Map authenticationMap 中模拟此代码;. . . public boolean createFirstTimerProfile(字符串密码,字符串用户名,字符串安全答案,字符串安全问题){

    String encryptedNewPassword = null;
    int userId;
    try {
        if(Util.isEmpty(password)||Util.isEmpty(userName)||Util.isEmpty(securityAnswer)||Util.isEmpty(securityQuestion))
            throw new CustomerEntryExceptions("values are null or empty");

        encryptedNewPassword =  encryptPassword(password);

        userId =(Integer.parseInt(authenticationMap.get("userid")));
        Connection con = Util.getConnection();
        if(clientUserManagement.addorupdatesecurityquestion(userName,securityQuestion, securityAnswer,userId,con)){
            String updatestatus = updateUserPassword(userId, encryptedNewPassword,
                     Boolean.parseBoolean(ServiceLocator.getConfigValue("authentication.fail_pwdchange_ind")),Integer.parseInt(ServiceLocator.getConfigValue("authentication.pwd_reason_firstlogin")));
            if(updatestatus.equals("updated")){
                velocityContextMembers = new HashMap<String,String>();
                velocityContextMembers.clear();
                velocityContextMembers.put("name", userName);
                velocityContextMembers.put("securityquestion", securityQuestion);
                velocityContextMembers.put("securityanswer", securityAnswer);
                velocityContextMembers.put("password", password);
                mailToUser(userId,"Authetication Profile Information","authenticationProfile_email_html.vm");
                return true;
            } else{
                throw new CustomerEntryExceptions("Password not updated");
            }
        } else{
            throw new CustomerEntryExceptions("Authentication profile not updated");
        }
    } catch (CustomerEntryExceptions e) {
        logger.error("createFirstTimerProfile() --> "+e.getMessage());
        return false;
    }

}

}

如何模拟行 Integer.Parseint(authenticationmap.get("userid");

4

1 回答 1

0

我同意 jan.vdbergh,我不确定嘲笑是最好的解决方案。

但是如果你想模拟一个静态方法可以这样处理:

@Test
public void myTestMethod(){
      new Expectations(){
          @Mocked 
          Integer integer= null;

          {
               Integer.parseInt(anyString);
               result = new NumberFormatException();

          }

       }
}
于 2013-10-30T12:48:19.760 回答