Junit mockito引发InvalidUseofMatcherException
我的getUserDetails类将User(custome类)和string作为参数并返回User。如果我使用Mockito matcher,如下所示:Junit mockito引发InvalidUseofMatcherException,junit,mockito,matcher,Junit,Mockito,Matcher,我的getUserDetails类将User(custome类)和string作为参数并返回User。如果我使用Mockito matcher,如下所示: when(authService.getUserDetails(any(User.class),anyString())).thenReturn(any(User.class)); 它给我提供了InvalidUseOfMatchers异常预期2个匹配器,发现3个。我不能使用上面的表达式吗?您应该将用户的实例传递给然后返回,而不是matche
when(authService.getUserDetails(any(User.class),anyString())).thenReturn(any(User.class));
它给我提供了InvalidUseOfMatchers异常预期2个匹配器,发现3个。我不能使用上面的表达式吗?您应该将
用户的实例
传递给然后返回
,而不是matcher。调用authService.getUserDetails
时将返回该User
实例。匹配器不用于返回
.thenReturn(any(User.class));
你必须在这里归还一些有形的东西。匹配器只是用于匹配输入,以便您可以指定在提供特定输入时返回的内容。您仍然需要有一个真实的输出才能返回。此代码可以工作:
User user=new User();
when(authService.getUserDetails(any(User.class),anyString())).thenReturn(user));
因为应该有一个值,而不是在thenReturns()中键入