2

I have been scratching my head a lot over something which just happened with Mockito.

I have this class, called ExamineFilter, which up to now has not been parameterised. 150+ tests to do with this class and others all passing fine. Lots of mocking.

I then changed ExamineFilter from

public class ExamineFilter extends FilteringTokenFilter implements GettableSet<UniqueSequence> {

to

public class ExamineFilter<V extends UniqueSequenceInterface> extends FilteringTokenFilter implements GettableSet<V> {

Now, when I have a spy of ExamineFilter, and go

spyExamineFilter.add( ... ) 

... this method add is not called but the code carries on. Whereas it was called before.

The add method here is in fact from Set, because GettableSet extends Set, and the signature of Set.add is

public boolean add( V element ){ ...

... each time this method is called on the spy it is returning false... which seems to be what a mock would do if such a boolean-returning method was being mocked.

I've also checked that this is indeed what is going on by finding out what happens if instead of using a spy of ExamineFilter<...> I use a real ExamineFilter<...>: and, indeed, add is called as normal.

Is there a known and documented explanation for this Mockito behaviour? Obviously I'm now trying to think of workarounds to rewrite the handful of tests which have now gone red as a result...

Addendum

By the way, for anyone interested, I tried both "flavours" of "callRealMethod":

    doCallRealMethod().when( spyExamineFilter ).add( any() ); // results in immediate call (i.e. at this line!) with null argument to add()
    when( spyExamineFilter.add( any())).thenCallRealMethod(); // results in mock "add" call when add() invoked
    when( spyExamineFilter.add( any( UniqueSequence.class ))).thenCallRealMethod(); // results in mock "add" call when add() invoked
    when( spyExamineFilter.add( notNull() )).thenCallRealMethod(); // results in mock "add" call when add() invoked

... if any passing Mockito high priests come this way: does any of the above suggest anomalous behaviour which might actually warrant raising an issue with the Mockito team?

mike rodent
  • 14,126
  • 11
  • 103
  • 157
  • you might wanna add the Annotation `@Override` to see if the method is truly overridding the base method, otherwise your IDE will give you a hint – Japu_D_Cret Mar 26 '17 at 16:25
  • can you show us the class definition for `GettableSet`? – Japu_D_Cret Mar 26 '17 at 16:26
  • Tx. Yep @Override tag is there all right. I spent an hour trying to find out whether in fact the "target" of add was something other than what I thought! GettableSet is an interface. You can find out what it's about because it comes from an answer of mine: http://stackoverflow.com/a/42657502/595305 – mike rodent Mar 26 '17 at 16:28

0 Answers0