MockitoException - ist ein *void-Methode* und *können* werden durch Stubs ersetzt, die mit einem *Rückgabewert*!

Ich bin mit Mockito für die Einheit testingand ich bin immer folgende exception.

org.mockito.exceptions.base.MockitoException: 
`'setResponseTimeStampUtc'` is a *void method* and it *cannot* be stubbed with a *return value*!
Voids are usually stubbed with Throwables:
    doThrow(exception).when(mock).someVoidMethod();
***

If you're unsure why you're getting above error read on.
Due to the nature of the syntax above problem might occur because:
1. The method you are trying to stub is *overloaded*. Make sure you are calling the right overloaded version.
2. Somewhere in your test you are stubbing *final methods*. Sorry, Mockito does not verify/stub final methods.
3. A spy is stubbed using `when(spy.foo()).then()` syntax. It is safer to stub spies - 
   - with `doReturn|Throw()` family of methods. More in javadocs for Mockito.spy() method.

Hier ist die eigentliche Klasse

@Repository
public class CardRepositoryImpl implements ICardRepository {        
    @Autowired
    private OperationBean operation;

    @Override
    public OperationBean getCardOperation(final String cardHolderId, final String requestTimeStamp) {
        operation.setRequestTimeStampUtc(requestTimeStamp);
        operation.setResponseTimeStampUtc(DateUtil.getUTCDate());
        return operation;
    }
}

dies ist der Test-Klasse, die ich geschrieben habe.

@RunWith(MockitoJUnitRunner.class)
public class CardRepositoryImplUnitTestFixture_Mockito {
    @InjectMocks
    private CardRepositoryImpl cardRepositoryImpl;
    private OperationBean operationBean;

    @Before
    public void beforeTest() {
        MockitoAnnotations.initMocks(this);
    }
    @Test
    public void canGetCardOperation(){
        when(cardRepositoryImpl.getCardOperation("2", Mockito.anyString())).thenReturn(operationBean);

    }    
}

Ist das ein Problem mit der return-Anweisung, da format () - Methode ist eine endgültige Methode.

public static String getUTCDate() {
    final TimeZone timeZone = TimeZone.getTimeZone("UTC");
    final Calendar calendar = Calendar.getInstance(timeZone);
    final SimpleDateFormat simpleDateFormat = new SimpleDateFormat("YYYY-MM-dd'T'HH:mm:ss.SSS'Z'");
    simpleDateFormat.setTimeZone(timeZone);
    return simpleDateFormat.format(calendar.getTime());
}

Wie kann ich dies beheben?

  • Ab in die Kommentare ich denke ich brauche zur Verwendung von Mock für CardRepositoryImpl, aber wenn ich mit Gespielter statt InjectMocks, sehe ich, dass mein test, den ich hier gepostet passieren, aber ich sehe keine Berichterstattung in meinem sonar code-coverage-UI für diese Methode. Abgesehen davon, dass, wenn ich ersetzen InjectMocks annotation mit Mock-annotation andere tests, die ich für die Klasse, die bereits gedeckt sind, Nicht bedeckt Stand
  • Möglich, Duplikat der Error: eine *andere* void-Methode nicht gekürzte mit einem Rückgabewert
InformationsquelleAutor Arun | 2016-02-12
Schreibe einen Kommentar