Last Updated: February 25, 2016
·
13.28K
· kinjacono

Mockito argument matchers and Scala

Attempting to use Mockito argument matchers in your Scala tests and getting a frustrating

Invalid use of argument matchers!

for no obvious reason?

Check that the function you're matching arguments on doesn't have any default values! All arguments need to be matchers, even if the values are provided by defaults:

def doCoolStuff(thing: Thing, message: String = "") = { ... }

Then the following will throw an InvalidUseOfMatchersException:

when(mock.doCoolStuff(eq(myThing)).thenReturn(anAwesomeResult)

But using a matcher for the defaulted parameters will work:

when(mock.doCoolStuff(eq(myThing),any[String])).thenReturn(anAwesomeResult)