2017-03-05 1 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("_","-"); 
    } 

Und ich habe diesen JMockit Test:JMockit MissingInvocation in spöttischen 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"); 
    } 


} 

Wenn ich diesen Test lief, zeigt die TextInput- und uiInput Werte als null. Kann mir bitte jemand sagen, warum die Werte nicht an die Methode findComponentInParents übergeben werden.

Ich erhalte diesen Fehler in den Protokollen:

mockit.internal.MissingInvocation: Missing 1 invocation to: 
javax.faces.component.UIInput#getSubmittedValue() 
    on mock instance: [email protected] 

was ist, dass ich hier in diesem Test bin fehlt? bitte hilfe.

Antwort

0

JMockit ist so dass Sie wissen, dass Sie nicht diese Methode aufgerufen wird, und nach dem Code, den Sie gemeinsam, Sie „erwarten“ einen Aufruf durch den Block mit:

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

Wenn das, was Sie wollen einen bestimmten Wert, ok, aber die Methode zurück, die Sie testen nicht rufen Sie die getSubmittedValue(), aber es nennen die getValue() Methode.

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

Vielleicht möchten Sie rief Ihre Erwartungen an die isValid() Verfahren auch für die getValue() werden hinzuzufügen.

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

      // ... more code 
    }}; 

Vergessen Sie nicht alles, was Sie in den statischen Block von Erwartungen erwähnt sollte mindestens einmal aufgerufen werden. Für weitere Informationen: http://jmockit.org/api1x/mockit/Expectations.html