我有一个简单的 Java 方法,我用它来测试来自 JSF 页面的输入。
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 测试感兴趣?我可以简单地调用它并传递参数,但没有返回值。有哪些可能的解决方案?