I am mocking an object with Mockito, the same method on this object is called multiple times and I want to return the same value every time.
This is what I have:
LogEntry entry = null; // this is a field
// This method is called once only.
when(mockLogger.createNewLogEntry()).thenAnswer(new Answer<LogEntry>() {
@Override
public LogEntry answer(InvocationOnMock invocationOnMock) throws Throwable {
entry = new LogEntry();
return entry;
}
});
// This method can be called multiple times,
// If called after createNewLogEntry() - should return initialized entry.
// If called before createNewLogEntry() - should return null.
when(mockLogger.getLogEntry()).thenAnswer(new Answer<LogEntry>() {
@Override
public LogEntry answer(InvocationOnMock invocationOnMock) throws Throwable {
return entry;
}
});
The problem is, it seems that my getLogEntry method is called only once. For all subsequent invocations, null
is returned instead and I get NPEs in tests.
How can I tell mockito to use stubbed version for all calls?
=================================================================
Post mortem for future generations
I did some additional investigation and as always it is not library's fault, it is my fault. In my code one of the methods called getLogEntry()
before calling createNewLogEntry()
. NPE was absolutely legitimate, the test actually found a bug in my code, not me finding bug in Mockito.
Your stub should work as you want it. From Mockito doc:
Once stubbed, the method will always return stubbed value regardless of how many times it is called.
Unless I'm missing something, if you want to return the same object for each method invocation then why not simple do:
final LogEntry entry = new LogEntry()
when(mockLogger.createNewLogEntry()).thenReturn(entry);
when(mockLogger.getLogEntry()).thenReturn(entry);
...
verify(mockLogger).createNewLogEntry();
verify(mockLogger, times(???)).getLogEntry();
Mockito will return the same value for every matching call.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With