mockito throwing InvalidUseOfMatchersException

蓝咒 提交于 2019-12-13 01:17:33

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!