-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Closed
Labels
Description
- The mockito message in the stacktrace have useful information, but it didn't help
- The problematic code (if that's possible) is copied here;
Note that some configuration are impossible to mock via Mockito - Provide versions (mockito / jdk / os / any other relevant information)
- Provide a Short, Self Contained, Correct (Compilable), Example of the issue
(same as any question on stackoverflow.com) - Read the contributing guide
Problematic version: 5.0.0, 5.10.0
JDK: 17
OS: MacOS
The following piece of code was fine with 4.x (I tried with 4.11.0). However, it throws exception after upgrading to 5.x. I think that's because the totalMemory
method is native. However, the exception message doesn't mention that.
public class FooTest {
@Test
public void fooTest() {
var runtime = mock(Runtime.class);
when(runtime.totalMemory()).thenReturn(1000L);
}
}
Exception message:
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 org.example.foo.FooTest.fooTest(FooTest.java:13)
at java.base/java.lang.reflect.Method.invoke(Method.java:580)
at java.base/java.util.ArrayList.forEach(ArrayList.java:1596)
at java.base/java.util.ArrayList.forEach(ArrayList.java:1596)