Jmockit测试失败,重载Validate()方法出现空指针异常


@Override
    public void validate(FacesContext facesContext, UIComponent uiComponent,
            Object emailId) throws ValidatorException {
        int userId = 0;
        try {
            AddNewUserDAO addNewUserDAO = new AddNewUserDAO();
            userId = addNewUserDAO.getUserIdWithUserName(emailId.toString());
        } catch (Exception exception) {
            LOGGER.error(exception);
        }
        matcher = pattern.matcher(emailId.toString());
        if (!matcher.matches()) {
            FacesMessage msg = new FacesMessage(new MessageProvider().getValue("prometheus_emailvalidation"));
            msg.setSeverity(FacesMessage.SEVERITY_ERROR);
            throw new ValidatorException(msg);
        }
        if (userId != 0) {
            FacesMessage msg = new FacesMessage(
                    new MessageProvider().getValue("prometheus_userexists"));
            msg.setSeverity(FacesMessage.SEVERITY_ERROR);
            throw new ValidatorException(msg);
        }
    }

我正试图用Jmockit写一个测试用例,但我得到空指针异常,在这种方法中,我们使用数据库查询(userId = addNewUserDAO.getUserIdWithUserName(emailId.toString());)这就是为什么无法模拟,如何模拟数据库查询和对象实例化的其他类在这样的验证器方法

Jmockit测试失败,重载方法出现空指针异常

就像这样在测试用例中模拟faces上下文。当退出电子邮件id时,您正在上升异常。所以在@Test中使用expected

    @Test(expected = javax.faces.validator.ValidatorException.class)
public void testValidatorWithExistedMailId(@Mocked FacesContext faces) {
    EmailIdExistanceValidator emailExistanceValidator=new EmailIdExistanceValidator();
    UIComponent uiComponent=null;
    emailExistanceValidator.validate(faces, uiComponent, "exitedId@gmail.com");
    assertNotNull(FacesMessage.VALUES);
}
@Test()
public void testValidatorWithUnExistedMailId(@Mocked FacesContext faces,@Mocked FacesMessage msg) {
    EmailIdExistanceValidator emailExistanceValidator=new EmailIdExistanceValidator();
    UIComponent uiComponent=null;
    emailExistanceValidator.validate(faces, uiComponent, (Object)"unexitedId@gmail.com");
    assertNotNull(FacesMessage.VALUES);
}

相关内容

  • 没有找到相关文章

最新更新