5

I'm trying to verify the following method gets called using Mockito:

class Notifier {
  def forward(request: ServletRequest)(onFailure: => Unit) : Unit
}

Here's the verification on a mock:

val notifier = mock[Notifier]
there was one(notifier).forward(any[ServletRequest])(any[() => Unit])

And I get the exception:

   The mock was not called as expected: 
    Invalid use of argument matchers!
    3 matchers expected, 2 recorded.
    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"));

I know this is caused by the last parameterless function. How can I perform a verify properly here?

Garrett Hall
  • 29,524
  • 10
  • 61
  • 76
  • 2
    `=> Unit` isn't a function type, but a by-name parameter. – Ben James Oct 02 '12 at 17:51
  • @BenJames so how is `(onFailure: Unit)` different from `(onFailure: => Unit)`? – David Moles Apr 02 '14 at 23:09
  • The difference is in when the block of code is executed. Having a parameter forward(onFailure: Unit) doesn't make sense. The argument would be evaluated before forward is invoked. With forward(onFailure: => Unit) method forward decides when to evaluate the argument. – Erik van Oosten Aug 27 '14 at 08:22
  • There is a mismatch in the method definition and the expectation. Method forward requires a by-name parameter, the expectation expects an argument that is a function with zero arguments. – Erik van Oosten Aug 27 '14 at 08:24

1 Answers1

2

Could you try Function0[Unit] ?

there was one(notifier).forward(any[ServletRequest])(any[Function0[Unit]])
Przemek
  • 7,111
  • 3
  • 43
  • 52