The answer by Tomasz Nurkiewicz appears not to tell the whole story!
NB Mockito version: 1.10.19.
I am very much a Mockito newb, so can't explain the following behaviour: if there's an expert out there who can improve this answer, please feel free.
The method in question here, getContentStringValue
, is NOT final
and NOT static
.
This line does call the original method getContentStringValue
:
doReturn( "dummy" ).when( im ).getContentStringValue( anyInt(), isA( ScoreDoc.class ));
This line does not call the original method getContentStringValue
:
doReturn( "dummy" ).when( im ).getContentStringValue( anyInt(), any( ScoreDoc.class ));
For reasons which I can't answer, using isA()
causes the intended (?) "do not call method" behaviour of doReturn
to fail.
Let's look at the method signatures involved here: they are both static
methods of Matchers
. Both are said by the Javadoc to return null
, which is a little difficult to get your head around in itself. Presumably the Class
object passed as the parameter is examined but the result either never calculated or discarded. Given that null
can stand for any class and that you are hoping for the mocked method not to be called, couldn't the signatures of isA( ... )
and any( ... )
just return null
rather than a generic parameter* <T>
?
Anyway:
public static <T> T isA(java.lang.Class<T> clazz)
public static <T> T any(java.lang.Class<T> clazz)
The API documentation does not give any clue about this. It also seems to say the need for such "do not call method" behaviour is "very rare". Personally I use this technique all the time: typically I find that mocking involves a few lines which "set the scene" ... followed by calling a method which then "plays out" the scene in the mock context which you have staged... and while you are setting up the scenery and the props the last thing you want is for the actors to enter stage left and start acting their hearts out...
But this is way beyond my pay grade... I invite explanations from any passing Mockito high priests...
* is "generic parameter" the right term?