2017-03-05 127 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