I have a code that I want to unit test. The code uses Collections.sort
method providing it with our own sweet comparator something like :
List<Something> something = somethingService.doSomething(someParameter);
Collections.sort(something, somethingComparator);
Now while testing the function I am mocking the somethingService
and stubbing the doSomething
method like :
List<Something> mockList = Mockito.mock(List.class);
Mockito.when(somethingService.doSomething(anyInt())).thenReturn(mockList);
and I am mocking the Collections as :
PowerMockito.mockStatic(Collections.class);
PowerMockito.doNothing().when(Collections.class, "sort", anyListOf(Something.class), anyOf(Comparator.class));
But it is giving me :
org.mockito.exceptions.misusing.InvalidUseOfMatchersException:
Misplaced argument matcher detected
You cannot use argument matchers outside of verification or stubbing.
Now I do know that if we are using argument matchers in any param of a function we need to provide matchers to all the params. But here is it possible to do the same and if not then what is the existing workaround?