Stubbing superclass' method using Mockito inline propagate to the real method call
See original GitHub issueHi my java env is
java version "1.8.0_74"
Java(TM) SE Runtime Environment (build 1.8.0_74-b02)
Java HotSpot(TM) 64-Bit Server VM (build 25.74-b02, mixed mode)
OS Version
MacOS Sierra 10.12.6
Mockito version:
mockito-inline:2.9.0
For this simple snippet:
public class StringList extends ArrayList<String>{
}
then stubbing the size() method
StringList stringList = mock(StringList.class);
doReturn(10).when(stringList).size();
stringList.size() will always return 0, and size() is acually called in ArrayList instread of the stub the above code works perfectly with mockito-core, for a workaround, StringList can be written in
public class StringList extends ArrayList<String>{
@Override
public int size(){
return 0;
}
}
then size() can be stubbed. I only need mockito inline to mock a single final class in one test case, but mockito-inline took over my entire module’s unit tesst, I’m wondering if there’s a way to choose mockito-core or inline for each test?
and I’m not sure if this is related, if stub in this way
when(stringList.size()).thenReturn(11);
Mockito reports
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:
- 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.
- inside when() you don’t call method on mock but on some other object.
Issue Analytics
- State:
- Created 6 years ago
- Comments:13 (12 by maintainers)
Top GitHub Comments
Ah, I just double-checked and it seems like the actual fix got lost in git rebase. I will fix this tonight!
We currently don’t publish automatically to Maven Central based on the feedback from community that there are too many versions of Mockito around 😃 We might get back to publishing every version as we did before.
I plan to ship 2.10 soon for https://github.com/mockito/mockito/pull/1121 so it will land in Maven Central!