Mokeito throwing InvalidUseOfMatchersException

My getUserDetails class accepts User (the custome class) and the string as arguments and returns the user. If I use mockito pairing as shown below:

when(authService.getUserDetails(any(User.class),anyString())).thenReturn(any(User.class));

It gives me an InvalidUseOfMatchersException. Expected 2 matches, 3 found. Can't I use the above expression?

+1
source share
3 answers

Matches are not used for refunds.

.thenReturn(any(User.class));

You must return something tangible here. Matches are intended only to coordinate input so that you can dictate what is returned when a specific input is provided. You still need to return the real result.

+2
source

User thenReturn, . User authService.getUserDetails.

+1

This code will work:

  User user=new User();
  when(authService.getUserDetails(any(User.class),anyString())).thenReturn(user));

Since there should be a value, not type in thenReturns ()

0
source

Source: https://habr.com/ru/post/1671648/


All Articles