Mockito: default return value
<p>According to the <a href="https://mockito.googlecode.com/hg-history/1.7/javadoc/org/mockito/Mockito.html" rel="nofollow">documentation</a>, non stubbed methods return null.
I want to test a method that should return "null" under some circumstances, however the test fails with the exception indicating the method was not called.</p>

<p>This is the test init function:</p>

<pre><code>@Before
public void setup() throws Exception {
    MockitoAnnotations.initMocks(this);
    mDataEntry = getFakeEntry();
    when(mRepository.getEntry(1L)).thenReturn(mDataEntry);
    mGetEntry = new GetEntryImpl(mRepository);
}
</code></pre>

<p>and this is the failed test:</p>

<pre><code>@SuppressWarnings("unchecked")
@Test
public void testGetEntry_failure() throws Exception {
    mGetEntry.execute(2L, mCallback);
    verify(mRepository).getEntry(eq(2L));
    verify(mCallback).onError(anyString(), Mockito.any(Exception.class));
}
</code></pre>

<p>the execute method calls the mocked object mRepository function getEntry(2L) which I was expecting to return null. However, this is what Mockito tells me when I run the test:</p>

<pre><code>Wanted but not invoked:
mRepository.getEntry(2);
-&gt; at com.xyz.interactor.GetEntryTest.testGetEntry_failure(GetEntryTest.java:54)
Actually, there were zero interactions with this mock.
</code></pre>

<p>I tried adding</p>

<pre><code>when(mRepository.getEntry(2L)).thenReturn(null);
</code></pre>

<p>to the init function, but it makes no difference. If I return a valid object instead of null, then the test fails as expected, because the onError function is not called (so the mocked object's function for value 2L is being called when I specify a valid return value).</p>

<p>How can I have the mocked object return null for a set of values?</p>

<p>Edit:</p>

<p>here's the code for the function under test:</p>

<pre><code>@Override
public void execute(final long id, final Callback&lt;DataEntry&gt; callback)  {
    AsyncTask.execute(new Runnable() {
        @Override
        public void run() {
            DataEntry dataEntry = mDataEntryRepository.getEntry(id);
            if (dataEntry != null) {
                callback.onResult(dataEntry);
            } else {
                callback.onError("TODO", null);
            }
        }
    });
}
</code></pre>

<p>and for reference, the success test works:</p>

<pre><code>@SuppressWarnings("unchecked")
@Test
public void testGetEntry_success() throws Exception {
    mGetEntry.execute(1L, mCallback);
    verify(mRepository).getEntry(eq(1L));
    verify(mCallback).onResult(eq(mDataEntry));
}
</code></pre>