Skip to content

Mockito anyListOf() List<List>

I’m using mockito-core:2.8.47 and Java 7 and want to use in a when and verify anyListOf or some other any method. My Problem is, if I just use anyList it says:

The method name( int, List < List < String > >) in the type Y is not
applicable for the arguments ( int, List < Object > )

How can I fix this?

ArgumentMatchers.anyListOf(ArgumentMatchers.anyListOf( String.class ) ) doesn’t work…

Answer

In my opinion you can get away with just the basic anyList() method with additional generics information:

Mockito.doReturn("1").when(classMock).name(ArgumentMatchers.eq(1)
                , ArgumentMatchers.<List<String>>anyList());

This worked for me and also remember to add the ArgumentMatcher for the first int variable otherwise Mockito will fail.