Suppose I have the following interface:
public interface ISomething { default int doStuff() { return 2 * getValue(); } int getValue(); }
When I am mocking this interface as follows:
@Mock private ISomething _something; @Before public void setup() { doCallRealMethod().when(_something).doStuff(); }
and try checking the doStuff () method as follows:
@Test public void testDoStuff() { when(_something.getValue()).thenReturn(42); assertThat("doStuff() returns 84", _something.doStuff(), is(84)); }
I expect the test to succeed, but I get:
org.mockito.exceptions.base.MockitoException: Cannot call real method on java interface. Interface does not have any implementation! Calling real methods is only possible when mocking concrete classes.
I tried subclassing ISomething
with an abstract class as follows:
public abstract class Something implements ISomething { }
and make fun of this class as stated above. With this approach, I get the same thing.
Does Mockito help support default calls?
source share