2013-04-02 6 views
9

This issue from 2010 hints at what I'm trying to do.Come si modifica il valore di ritorno predefinito per le stringhe in Mockito?

Sto lavorando a un test di unità che esercita codice che richiede che molti oggetti derisi facciano ciò che deve fare (test di rendering HTML + PDF). Perché questo test abbia successo ho bisogno di generare molti oggetti falsati e ognuno di questi oggetti restituisce alla fine alcuni dati String al codice in fase di test.

io penso posso fare questo implementando sia la mia stessa classe Answer o IMockitoConfiguration, ma io non sono sicuro di come implementare quelle in modo che influiscono solo metodi che restituiscono stringhe.

Ritengo che il seguente codice sia vicino a quello che voglio. Genera un'eccezione cast, java.lang.ClassCastException: java.lang.String cannot be cast to com.mypackage.ISOCountry. Penso che questo significhi che in qualche modo ho bisogno di default o limitare lo Answer a influenzare solo i valori predefiniti per String.

private Address createAddress(){ 
    Address address = mock(Address.class, new StringAnswer()); 

    /* I want to replace repetitive calls like this, with a default string. 
    I just need these getters to return a String, not a specific string. 

    when(address.getLocality()).thenReturn("Louisville"); 
    when(address.getStreet1()).thenReturn("1234 Fake Street Ln."); 
    when(address.getStreet2()).thenReturn("Suite 1337"); 
    when(address.getRegion()).thenReturn("AK"); 
    when(address.getPostal()).thenReturn("45069"); 
    */ 

    ISOCountry isoCountry = mock(ISOCountry.class); 
    when(isoCountry.getIsocode()).thenReturn("US"); 
    when(address.getCountry()).thenReturn(isoCountry); 

    return address; 
} 

//EDIT: This method returns an arbitrary string 
private class StringAnswer implements Answer<Object> { 
    @Override 
    public Object answer(InvocationOnMock invocation) throws Throwable { 
     String generatedString = "Generated String!"; 
      if(invocation.getMethod().getReturnType().isInstance(generatedString)){ 
       return generatedString; 
      } 
      else{ 
       return Mockito.RETURNS_DEFAULTS.answer(invocation); 
      } 
     } 
} 

Come posso configurare Mockito per restituire una stringa generata per impostazione predefinita per i metodi di una classe deriso che tornare stringa? I found a solution to this part of the question on SO

Per i punti extra, come posso fare in modo che il valore generato sia una stringa che si trova nel formato Class.methodName? Ad esempio "Address.getStreet1()" invece di una stringa casuale?

risposta

8

Sono stato in grado di rispondere completamente alla mia domanda.

In questo esempio viene generato un indirizzo con la località di Louisville mentre gli altri campi hanno l'aspetto "address.getStreet1();".

private Address createAddress(){ 
    Address address = mock(Address.class, new StringAnswer()); 

    when(address.getLocality()).thenReturn("Louisville"); 

    ISOCountry isoCountry = mock(ISOCountry.class); 
    when(isoCountry.getIsocode()).thenReturn("US"); 
    when(address.getCountry()).thenReturn(isoCountry); 

    return address; 
} 

private class StringAnswer implements Answer<Object> { 
    @Override 
    public Object answer(InvocationOnMock invocation) throws Throwable { 
      if(invocation.getMethod().getReturnType().equals(String.class)){ 
       return invocation.toString(); 
      } 
      else{ 
       return Mockito.RETURNS_DEFAULTS.answer(invocation); 
      } 
     } 
} 
Problemi correlati