2

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?

gontard
  • 28,720
  • 11
  • 94
  • 117
Sourabh
  • 1,253
  • 6
  • 21
  • 39
  • @Community : Please help :) – Sourabh Jul 15 '14 at 12:31
  • 1
    This is strange. I get the same problem. But this [similar answer](http://stackoverflow.com/a/22991435/1594933) works – gontard Jul 15 '14 at 12:51
  • @Gontard : I have also tried most of the workarounds available on the web but in vain. I even tried spying but then again they give a different problem. Right now all I can think of he providing fakes instead of mocks and making the unit test case run. – Sourabh Jul 15 '14 at 14:11
  • Yes you could avoid to mock the list and stub the sort method. – gontard Jul 15 '14 at 14:32
  • 1
    Why do you need to mock Collections anyway? What exactly are you testing? – David M. Karr Jul 15 '14 at 14:56
  • I wanted to test my flow as well as the comparator in the single flow. If my list is getting sorted out properly using my comparator for a series of test cases I would not be required to test the comparator as a unit again. – Sourabh Jul 16 '14 at 05:51
  • Could you use java 8 ? – gontard Jul 16 '14 at 07:52
  • No :( but what feature does it provide?\ – Sourabh Jul 16 '14 at 08:00
  • There is [sort method on list](http://docs.oracle.com/javase/8/docs/api/java/util/List.html#sort-java.util.Comparator-). This method is not static so the usage of powermock could be avoided. – gontard Jul 16 '14 at 08:22

1 Answers1

0

You are using PowerMock to mock a system class from the JDK, design wise it's really, really nasty. Seriously one should really think over his motivation to use Powermock as the author himself is.

Powermock cannot really mock directly static methods from system class, you have to create wrappers around the system calls, as documented in the wiki.

I would strongly advises you to extract some sorting strategy, that you could mock with just Mockito.

bric3
  • 40,072
  • 9
  • 91
  • 111
  • 1
    The wiki entry show how to mock `URLEncoder`. So why it doesn't work with `Collections` ? – gontard Jul 15 '14 at 19:30
  • I think you need to wrap the call. However without the complete code we cannot help you diagnose much more than that. – bric3 Jul 16 '14 at 07:53