MissingMethodInvocationException is RuntimeException and subclass of MockitoException. It generally throws when:
- It might occurs when() requires an argument which has to be ‘a method call on a mock. For example: when(mock.getArticles()).thenReturn(articles);
- You try to stubbed/verified either of: final/private/equals()/hashCode() methods.
- Mocking methods declared on non-public parent classes is not supported.
- Inside when() you don’t call method on real object always call from mock or spy.
Constructors
- MissingMethodInvocationException(String message) : Will throw exception with message.
Example
To complete example follow link Mockito + JUnit Tutorial
import static org.mockito.Mockito.spy; import org.junit.Assert; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import static org.mockito.Mockito.*; //@RunWith attaches a runner with the test class to initialize the test data @RunWith(org.mockito.runners.MockitoJUnitRunner.class) public class MathApplicationTestrSpy { private CalculatorService calcService; @Before public void setUp(){ Calculator calculator = new Calculator(); calcService = spy(calculator); //Issue is here because using actual object when(calculator.add(20.0,10.0)).thenReturn(30.0); } @Test public void testAdd(){ Assert.assertEquals(calcService.add(20.0, 10.0),30.0,0); } }
Output
org.mockito.exceptions.misusing.MissingMethodInvocationException: when() requires an argument which has to be 'a method call on a mock'. For example: when(mock.getArticles()).thenReturn(articles); Also, this error might show up because: 1. you stub either of: final/private/equals()/hashCode() methods. Those methods *cannot* be stubbed/verified. Mocking methods declared on non-public parent classes is not supported. 2. inside when() you don't call method on mock but on some other object. at com.facingissuesonit.mockito.MockitoTestExamples.MathApplicationTestrSpy.setUp(MathApplicationTestrSpy.java:29) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:497) at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15) at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42)
Solutions
Here in above example trying stub on real object instead of mock and spy object. To solve this issue update code as below. For complete example please refer Mockito + JUnit TutorialMockito + JUnit Tutorial.
@Before public void setUp(){ Calculator calculator = new Calculator(); calcService = spy(calculator); //Issue is here because using actual object //when(calcService.add(20.0,10.0)).thenReturn(30.0); //Used Spy object when(calculator.add(20.0,10.0)).thenReturn(30.0); }
References
Know More
To know more about Junit, Mockito and exception solutions follow below links:
In the solution, shouldn’t it be the other way round and the call to calculator be in the upper comment (because it was the original issue), while the clall to calcService is the working solution?