Matching on Varargs with Mockito

November 13, 2016

Implimenting a custom Mockito 1.X Varargs Matcher

I recently had the need for a Mockito matcher, like eq() , to match on varargs. There is anyVararg() provided by the library but nothing for actually matching on the contents of the array. After some digging I found that you need to impliment a custom matcher as described on Stackoverflow here.

The solution is to impliment a custom matcher that impliments the VarargMatcher interface

This can then be wrapped in a static factory method if you want something that looks more like eq

For those interested there is an open issue you can watch for adding this to the library proper.