In latest version of Mockito org.mockito.ArgumentMatchers.anyListOf is missing. Do you know, for Junit 5, how to replace it?
2 Answers
The simple answer is to replace anyListOf(SomeClass.class) with anyList().
The anyListOf method was deprecated at some point in Mockito 2.x and then removed in Mockito 3.0. As the Mockito 2.2.7 javadoc stated:
Deprecated. With Java 8 this method will be removed in Mockito 3.0. This method is only used for generic friendliness to avoid casting, this is not anymore needed in Java 8.
Any non-null List. Generic friendly alias to
anyList(). It's an alternative to@SuppressWarnings("unchecked")to keep code clean of compiler warnings.This method doesn't do type checks of the list content with the given type parameter, it is only there to avoid casting in the code.
You also asked:
It will be great of I can specify the expected object type like
anyListOf. Any other alternatives?
As the javadocs stated, anyListOf didn't check the types anyway.
Alternatives? Well you could write your own version of anyListOf that does check the list elements, and add that to your (test) codebase.
Comments
As in Stephen C's answer, anyListOf is no longer necessary since Generalized Target-Type Inference in Java 8 allows Java to infer the generic argument.
// for YourMockedInterface.someMethod(List<YourListItem> yourList) when(someMock.someMethod(anyListOf(YourListItem.class))).thenReturn(...); // Java 5-7 when(someMock.someMethod(anyList() ).thenReturn(...); // Java 8+ You can still use the same syntax from Java 5+ to specify the type parameter explicitly; however, you will have to list the AdditionalMatchers class, as static imports are not supported for explicit type arguments.
when(someMock.someMethod(AdditionalMatchers.<YourListItem>anyList()).thenReturn(...);
anyList. The deprecation message in the (older) javadocs explains it thus: "With Java 8 this method will be removed in Mockito 3.0. This method is only used for generic friendliness to avoid casting, this is not anymore needed in Java 8."Expected 0 arguments but found 1because I need to pass argument.anyListandanyListOfthat theClassargument is NOT used for type checking. It is just there to deal with Java limitations prior to Java 8.anyListOf(Class)didn't check that the list members were all of the given a class anyway. So changing toanyListis not losing anything.