In Mockito / Scala, verify method with unused varargs parameter

Viewed 244

I have a method with this signature:

def foo(param1: => String, param2: (String, String)*)(implicit param3: Context): Unit

In my code, I call it as

foo("bar") // no varargs, implicit is in scope

In my Unit test, I am trying to verify the call:

  verify(mock).foo(stringCaptor.capture())(any[Context])

This compiles, but produces a runtime exception:

Invalid use of argument matchers! 3 matchers expected, 1 recorded: -> at com.mycompany.MySpec$$anon$3.(MySpec.scala:88) This exception may occur if matchers are combined with raw values:
//incorrect: someMethod(anyObject(), "raw String"); When using matchers, all arguments have to be provided by matchers. For example: //correct:
someMethod(anyObject(), eq("String by matcher")); For more info see javadoc for Matchers class. org.mockito.exceptions.misusing.InvalidUseOfMatchersException: Invalid use of argument matchers! 3 matchers expected, 1 recorded:

Yet if I try to match the varargs parameter, I get compilation errors:

  verify(mock).foo(stringCaptor.capture(), any[Seq[(String, String)]])(any[Context])

Cannot resolve overloaded method 'foo'

What would be the syntax to match my method and verify the call correctly?

Note: answers to other questions suggest adding a dependency to mockito-scala, which I'd like to avoid

Update: adding a matcher for a tuple reduces the missing matchers from 1 to 2:

  verify(mock).foo(stringCaptor.capture(),any[(String, String)])(any[Context])

Invalid use of argument matchers! 3 matchers expected, 2 recorded: [...]

(it doesn't make a difference if I replace the ArgumentCaptor with anyString())

Another Update:

Ah, it seems the => String part causes the problem, didn't realize that I'm trying to match a lazy string here. Under the hoods, the compiler seems to turn my String into a Function0[String], but how can I match that?

0 Answers
Related