Jmockit 测试失败,Override Validate() 方法出现 null ponter 异常
Jmockit tests fail with null ponter exception for Override Validate() method
@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 测试失败,Override 方法出现 null ponter 异常
像这样模拟测试用例中的面孔上下文。退出 emailId 时您正在上升异常。所以在 @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);
}
@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 测试失败,Override 方法出现 null ponter 异常
像这样模拟测试用例中的面孔上下文。退出 emailId 时您正在上升异常。所以在 @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);
}