user8867007
user8867007

Reputation: 1

Mockito matcher doesn't recognise abstract classes in arguments

this is a sample code

when(someObject.someMethod(any(AbstractClass.class)).thenReturn(mockvalue);

in the above code, it isn't recognising the argument any(AbstractClass.class) and it calls the real method instead of returning the mock value.

Upvotes: 0

Views: 449

Answers (1)

Florian Schaetz
Florian Schaetz

Reputation: 10652

I am sorry, but you are on the wrong track there.

any( SomeClass.class ) does NOT do what you believe it does. It especially does NOT check if the argument is a SomeClass, see the Javadoc:

Any kind object, not necessary of the given class.
The class argument is provided only to avoid casting.

If you have a look at the Any class, you will see why:

public boolean matches(Object actual) {
    return true;
}

So, ANY argument (as the name implies) will be accepted there. In your case, this means that IF the method someMethod on that specific someObject is called, it WILL return the mockvalue, no matter what the actual argument is.

This implies that your problem is somewhere else entirely, for example ...

  • Your mock is not correctly injected into the class you are testing (so that the class is using another object and not the mock)
  • The method in question isn't actually called (for example there could be another one with a similar signature, etc.).

Hard to say without code. I would ask a new question but provide more code this time.

Upvotes: 1

Related Questions