0
public abstract class AbstractValidator implements Validator
{
@Override
public abstract void validate(
FacesContext context, UIComponent component, Object value)
throws ValidatorException;
protected String buildValidationMessage(String param)
{
return param;
}
}
public abstract class AbstractMatchValidator extends AbstractValidator {
protected String field1ComponentId;
protected String field1InputLabel;
protected String inputLabel;
protected abstract String cleanBeforeComparing(String value);
@Override
public void validate(FacesContext context,
UIComponent component,
Object value) throws ValidatorException {
UIInput input1 = Components.findComponentInParents(component,
field1ComponentId);
if (null == input1) {
return;
}
String field1 = null;
if (input1.isValid()) {
field1 = this.cleanBeforeComparing((String)input1.getValue());
}
String confirmField = this.cleanBeforeComparing((String) value);
if (!StringUtils.isBlank(field1) && !field1.equals(confirmField)) {
input1.setValid(false);
FacesMessage msg = buildValidationMessage(context,
this.inputLabel,
this.fieldToMatchInputLabel);
throw new ValidatorException(msg);
}
}
public class EmployeeIDMatchValidator extends AbstractFieldMatchValidator {
protected String cleanBeforeComparing(String value) {
return value.replace("_",":");
}
public class SSNMatchValidator extends AbstractFieldMatchValidator {
protected String cleanBeforeComparing(String value) {
return value.replace("_","-");
}
而且我有這個Jmockit測試:Jmockit MissingInvocation在嘲笑findComponentInParents
@RunWith(JMockit.class)
public class EmployeeIDMatchValidatorTest {
@Tested
EmployeeIDMatchValidator validator;
@Test
@SuppressWarnings(value = "unchecked")
public final void testNoExceptionIsThrownForNoEmployeeIDValue(
@Mocked FacesContext facesContext,
@Mocked UIInput textInput,
@Mocked UIInput uiInput){
new MockUp<Components>() {
@Mock
public <C extends UIComponent> C findComponentInParents(UIComponent component, String clientId) {
return (C) textInput;
}
};
new Expectations() {{
textInput.getSubmittedValue();
result="9-9192121-1";
MessageFormat.format(anyString, any);
result = "Employee ID is invalid.";
}};
validator.validate(facesContext, uiInput, "9-91921211");
}
}
當我運行這個測試,TextInput和uiInput值顯示爲空。有人可以告訴我爲什麼值不會傳遞給findComponentInParents方法。
我在日誌中收到此錯誤:
mockit.internal.MissingInvocation: Missing 1 invocation to:
javax.faces.component.UIInput#getSubmittedValue()
on mock instance: [email protected]
是什麼,我很想念這裏在本次測試?請幫忙。