2013-04-02 9 views
9

This issue from 2010 hints at what I'm trying to do.Jak zmienić domyślną wartość zwracaną dla Ciągi w Mockito?

Pracuję nad testem jednostkowym, który wykonuje kod, który wymaga wielu wyśmianych obiektów, aby zrobić to, co trzeba (testowanie renderowania HTML + PDF). Aby ten test zakończył się sukcesem, potrzebuję wielu wyśmianych obiektów, a każdy z tych obiektów ostatecznie zwróci niektóre dane Stringa do testowanego kodu.

myślę mogę to zrobić poprzez wdrażanie zarówno własne Answer klasę lub IMockitoConfiguration, ale nie jestem pewien, jak wdrożyć te wpływają więc one jedynie metody, które zwracają ciągi.

Czuję, że poniższy kod jest zbliżony do tego, co chcę. Zgłasza wyjątek odrzucający, java.lang.ClassCastException: java.lang.String cannot be cast to com.mypackage.ISOCountry. Myślę, że oznacza to, że muszę jakoś domyślnie lub ograniczyć wartość Answer, aby wpłynąć tylko na wartości domyślne dla 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); 
      } 
     } 
} 

Jak mogę ustawić Mockito, aby domyślnie zwracał wygenerowany łańcuch dla metod na wyśmiewanej klasie, która zwraca String? I found a solution to this part of the question on SO

Dla dodatkowych punktów, w jaki sposób mogę uczynić, że wygenerowana wartość będzie ciągiem, który ma postać Class.methodName? Na przykład "Address.getStreet1()" zamiast tylko losowy ciąg?

Odpowiedz

8

Udało mi się całkowicie odpowiedzieć na własne pytanie.

W tym przykładzie generowany jest adres z miejscowości Louisville, podczas gdy pozostałe pola wyglądają jak "adres.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); 
      } 
     } 
}