user1052610
user1052610

Reputation: 4719

Mockito: creating a parameter matcher for a class

Given a method definition as follows:

MyClass.myMethod(SecondClass secondClass);

and a mock of MyClass:

MyClass myClass = mock(MyClass.class);

how would you match the method parameter when defining the expecation?

when(myClass.myMethod(???)).thenReturn(null);

Thanks

Upvotes: 1

Views: 79

Answers (3)

koljaTM
koljaTM

Reputation: 10262

You can use

Mockito.any(SecondClass.class)

or

(SecondClass)any()

Upvotes: 1

Guillaume
Guillaume

Reputation: 22812

when(myClass.myMethod(Mockito.any(SecondClass.class))).thenReturn(null);

Upvotes: 1

iberbeu
iberbeu

Reputation: 16185

Actually the best way to do it is

doReturn(object).when(myClass).myMethod(???);

in ??? you have some possibilities.

  • You can pass a given object and then you wait for an especific one

  • Or you can pass Mockito.any(Clazz.class) and then you will accept any object of that kind

Upvotes: 0

Related Questions