mockito-使用值列表中的一个值来匹配matcher

Amo*_*wal 5 junit matcher mockito

我的方法界面是

Boolean isAuthenticated(String User)
Run Code Online (Sandbox Code Playgroud)

我想从值列表中比较,如果任何用户从列表中传递给函数,那么它应该返回true.

when(authService.isAuthenticated(or(eq("amol84"),eq("arpan"),eq("juhi")))).thenReturn(true);
Run Code Online (Sandbox Code Playgroud)

我正在使用额外的参数匹配'或'但上面的代码不起作用.我该如何解决这个问题?

Jef*_*ica 7

or没有三参数重载.(请参阅文档.)如果您的代码编译,您可能导入的or方法不同于org.mockito.AdditionalMatchers.or.

or(or(eq("amol84"),eq("arpan")),eq("juhi")) 应该管用.

您也可以尝试通过Mockito匹配器访问的isOneOfHamcrest匹配argThat:

when(authService.isAuthenticated(argThat(isOneOf("amol84", "arpan", "juhi"))))
    .thenReturn(true);
Run Code Online (Sandbox Code Playgroud)