Search code examples
scalamockitoscalatest

Verifying by-name parameters in Mockito


Given

class UnderTest {
    def f(arg1: Int)(arg2: => Int) = ???
}

Trying do do this:

import org.mockito.Matchers
val objUnderTest = mock[UnderTest]
verify(objUnderTest).f(Matchers.eq(1))(Matchers.any())

fails with an "Invalid use of argument matchers!" exception, complaining that 2 matchers were expected, 1 was recorded.

Is using Mockito to verify calls to functions with multiple argument lists including by-name parameters possible?


Solution

  • To my knowledge you can not mock byname parameters with Mockito. I've done it in specs2 but that requires to override some of the Mockito classes, which makes it possible but it is an ugly solution.