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

标签 java jmockit

我如何在 jmockit 中模拟这段代码 A类{ 私有(private) map authenticationMap; . . . public boolean createFirstTimerProfile(String password, String userName, String securityAnswer, String securityQuestion){

    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");

最佳答案

我同意 jan.vdbergh 的观点,我不确定模拟是最好的解决方案。

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

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

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

          }

       }
}

关于java - 如何模拟行 Integer.Parseint(authenticationmap.get ("userid");,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12582396/

相关文章:

java - 加载图像并在加载完成后进行设置

java - 如何使用 JMockit 1.31 模拟非静态方法

android - Robolectric + JMockIt 设置

java - 如何使用 JMockit 模拟 Date 类的默认构造函数?

java - 更改构造函数,缺少 jrebel '-noverify'

java - 调用 Spring SecurityContextHolder 时出现 NullPointerException

java - 如何使用 XPath 匹配以数字结尾的字符串

sbt - SBT 可以与 jMockit 一起使用吗?

unit-testing - JMockit - 模拟 CXF web 服务端口对象

java - DEX 和 Dalvik 是否支持 Java 二进制兼容性?