Mockito NotaMockException
As you noted, act
is not a mock, and therefore you cannot record behavior on it. You could use Mockito.spy
to, well, spy (or partially mock) the act
object so that you only record the behavior of secondMethod
and execute the actual code for firstMethod
.
Note, however, that matchers can't be used in doReturn
calls regardles of how you're mock
ing or spy
ing your object. A return value must be a concrete object.
class ActivityTest() {
Activity act;
@Before
public void setup(){
act = Mockito.spy(new Activity()); // Here!
}
@Test
public void testFirstMethod(){
Mockito.doReturn("someString").when(act).secondMethod();
act.firstMethod();
verify(act).secondMethod();
}
}
A slightly more elegant syntax allows you to use annotations instead of explicitly calling Mockito.spy
, but it's a matter of taste really:
@RunWith(MockitoJUnitRunner.class)
class ActivityTest() {
@Spy
Activity act = new Activity();
@Test
public void testFirstMethod(){
Mockito.doReturn("someString").when(act).secondMethod();
act.firstMethod();
verify(act).secondMethod();
}
}