Mockito - calling real method

e.g.

class C {
 void m1() { ...}
 boolean m2() { ... return flag;}
}   

unit test code:

C cMock = Mockito.mock(C.class);
Mockito.doNothing().when(cMock).m1();
Mockito.when(cMock.m2()).thenCallRealMethod();
The strange thing is that m2 is not being called.


Solution


This is also where Mockito.spy can be used. it allows you to do partial mocks on real objects.

C cMock = Mockito.spy(new C());
Mockito.doNothing().when(cMock).m1();