1

In latest version of Mockito org.mockito.ArgumentMatchers.anyListOf is missing. Do you know, for Junit 5, how to replace it?

5
  • Replace it with 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." Commented Nov 8, 2024 at 0:00
  • I get error Expected 0 arguments but found 1 because I need to pass argument. Commented Nov 8, 2024 at 2:07
  • "... because I need to pass argument." - No you don't. That's the whole point of the deprecation / removal. Remove the argument. It should be clear from reading the (full!!) javadoc for anyList and anyListOf that the Class argument is NOT used for type checking. It is just there to deal with Java limitations prior to Java 8. Commented Nov 8, 2024 at 2:12
  • it will be great of I can specify the expected object type like anyListOf. Any other alternatives? Commented Nov 8, 2024 at 3:01
  • AFAIK, no. But put it this way, anyListOf(Class) didn't check that the list members were all of the given a class anyway. So changing to anyList is not losing anything. Commented Nov 8, 2024 at 3:36

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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(...); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.