I am getting problem with Mockito trying to call real method while stubbing. I assume this might be related to that method is inherited. This is component external to my system and can't do really much about it. But let's get to the code
AbstractRpcClient abstractRpcClient = mock(AbstractRpcClient.class);
doNothing().when(abstractRpcClient).callOnce(anyString(), anyVararg());
Since callOnce
calls methods on some other objects I get NPE right in the second line.
AbstractRpcClient
inherits from another abstract class, but this class is package local, so I can't even cast it in my stubbing.
Is there anything I can do about it? How can I stub this method to do nothing or throw exception WITHOUT calling real method.
Do I have to extend this class in test and override method callOnce
? This of course works, but is there any other solution?
Minimal example:
package external.component;
public abstract class ClassToMock extends SuperClass {
}
abstract class SuperClass {
public void callOnce(String method, Object... params) {
throw new RuntimeException("We shouldn't be here");
}
}
Test class
package mypackage
import static org.mockito.Matchers.anyString;
import static org.mockito.Matchers.anyVararg;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.mock;
public class Testclass {
@Test
public void test() {
external.component.ClassToMock classToMock = mock(external.component.ClassToMock.class);
doNothing().when(classToMock).callOnce(anyString(), anyVararg());
}
}
In example of course RuntimeException
is thrown from callOnce
method.