我有这个简单的Java方法,用于测试来自JSF页面的输入。JSF验证器的JUnit测试
public void validateDC(FacesContext context, UIComponent component, Object value) throws ValidatorException, SQLException
{
Double d;
String s = value.toString().trim();
if (s.length() > 10)
{
throw new ValidatorException(new FacesMessage(FacesMessage.SEVERITY_ERROR,
" Value is too long! (18 digits max)", null));
}
try
{
d = Double.parseDouble(s);
if (d < 0)
{
throw new ValidatorException(new FacesMessage(FacesMessage.SEVERITY_ERROR,
" '" + d + "' must be positive number!", null));
}
}
catch(NumberFormatException nfe) { d = null; }
if (d != null)
{
}
else
{
throw new ValidatorException(new FacesMessage(FacesMessage.SEVERITY_ERROR,
s.isEmpty() ? " This field cannot be empty!" : " '" + s + "' is not a number!", null));
}
}
我很感兴趣如何为此验证器编写JUnit测试?我可以简单地调用它并传递参数,但没有返回值。什么是可能的解决方案?
验证方法的一般合同在[Validator javadoc](http://docs.oracle.com/javaee/6/api/javax/faces/validator/Validator.html)中有描述。 – McDowell