问题
My getUserDetails class take User(custome class) and string as arguments and return User. If I use Mockito matcher as below:
when(authService.getUserDetails(any(User.class),anyString())).thenReturn(any(User.class));
It gives me InvalidUseOfMatchersException 2 matchers expected, 3 found. Can't I use the above expression?
回答1:
Matchers are not used for returning.
.thenReturn(any(User.class));
You have to return something tangible here. Matchers are just for matching up input so that you can dictate what is returned when certain input is provided. You still need to have a real output to return.
回答2:
You should pass instance of User
to thenReturn
, not matcher. That User
instance will be returned when authService.getUserDetails
is invoked.
回答3:
This code will work:
User user=new User();
when(authService.getUserDetails(any(User.class),anyString())).thenReturn(user));
As there should be a value and not type in thenReturns()
来源:https://stackoverflow.com/questions/36771597/mockito-throwing-invaliduseofmatchersexception