Search code examples
junitmockitomatcher

mockito -using one of the values from list of values to compare in matcher


My method interface is

Boolean isAuthenticated(String User)

I want to compare from list of values if any of the users are passed in the function from the list, then it should return true.

when(authService.isAuthenticated(or(eq("amol84"),eq("arpan"),eq("juhi")))).thenReturn(true);

I am using additional argument matcher 'or' but above code is not working. How can I resolve this issue?


Solution

  • or does not have a three-argument overload. (See docs.) If your code compiles, you may be importing a different or method than org.mockito.AdditionalMatchers.or.

    or(or(eq("amol84"),eq("arpan")),eq("juhi")) should work.

    You might also try the oneOf Hamcrest matcher (previously isOneOf), accessed through the argThat Mockito matcher:

    when(authService.isAuthenticated(
             argThat(is(oneOf("amol84", "arpan", "juhi")))))
        .thenReturn(true);