0

I have a question what about Argument Matcher.

class A(){
   public B method(Class T,String str){} 
}

I Stub the method and want pass the method.but .

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: Invalid use of argument matchers!

A a = new A();
B b = new B();
Mockito.doReturn(b).when(a).method(argThat(new IsClass)), "111");

Class IsClass:

class IsClass extends ArgumentMatcher<Class> {  
    public boolean matches(Object obj) {  
        return true;  
    }  
} 

So, how should I do, can pass this method.Thankyou.

Hamza Anis
  • 2,475
  • 1
  • 26
  • 36
yujia
  • 15
  • 1
  • 5

1 Answers1

4

The full exception message should tell you what is wrong:

This exception may occur if matchers are combined with raw values:

//incorrect: someMethod(anyObject(), "raw String");

So, if your isClass() were a valid ArgumentMatcher then you would stub like this:

Mockito.doReturn(b).when(a).method(argThat(new IsClass()), eq("111"));
//note how the second parameter of method uses the argument matcher
//"eq" rather than the raw string "111"

Moreover, if you merely want to match "any class object" you can do it like this without having to write your own custom matcher:

Mockito.doReturn(b).when(a).method(any(Class.class), eq("111"));

Finally, you can only stub for mocks. So your test would have to include some setup code like this:

A a = Mockito.mock(A.class);
B b = new B();
Mockito.doReturn(...

Consider spending some time reading through the documentation to get a better handle of how to test using Mockito.

David Rawson
  • 20,912
  • 7
  • 88
  • 124