2017-03-05 424 views
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] 

是什么,我很想念这里在本次测试?请帮忙。

回答

0

JMockit是让你知道,你不调用该方法,并根据您共享,您使用了块“期待”一个调用的代码:

new Expectations() {{ 
      textInput.getSubmittedValue(); 
       result="9-9192121-1"; ... 
    }}; 

如果你想要什么返回的特定值不错,但你要测试不会调用getSubmittedValue()的方法,但它调用的getValue()方法。

new Expectations() {{ 
       uiInput.getValue(); 
        result="9-9192121-1"; 
    //... more code 
}}; 

您可能要添加到您的期望isValid()的方法也为的getValue()被调用。

new Expectations() {{ 
      uiInput.isValid(); result = true; 
      uiInput.getValue(); result="9-9192121-1"; 

      // ... more code 
    }}; 

不要忘记你在静态的期望块内提到的一切应该至少调用一次。欲了解更多信息: http://jmockit.org/api1x/mockit/Expectations.html