question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Stubbing superclass' method using Mockito inline propagate to the real method call

See original GitHub issue

Hi 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:

  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.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:13 (12 by maintainers)

github_iconTop GitHub Comments

2reactions
raphwcommented, Sep 5, 2017

Ah, I just double-checked and it seems like the actual fix got lost in git rebase. I will fix this tonight!

1reaction
mockitoguycommented, Sep 8, 2017

Should be published automatically. @szczepiq?

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!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Mockito How to mock only the call of a method of the superclass
I'm using Mockito in some tests. ... I want to mock only the second call ( super.save ) of ChildService . The first...
Read more >
Mockito (Mockito 4.9.0 API) - Javadoc.io
Spying on real objects. You can create spies of real objects. When you use the spy then the real methods are called (unless...
Read more >
Mockito verify superclass method called
Stubbing superclass ' method using Mockito inline propagate to the real method call #1180. you don't call method on mock but on some...
Read more >
New API for clearing mock state in inline mocking (Since 2.25.0)
Note 1: Stubbing partial mocks using when(mock.getSomething()).thenReturn(fakeValue) syntax will call the real method. For partial mock it's ...
Read more >
when() requires an argument which has to be 'a method call ...
mockito/mockitoStubbing superclass' method using Mockito inline propagate to the real method call#1180. Created over 5 years ago. 13. Hi my java env is....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found