3

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?

MHarris
  • 1,801
  • 3
  • 21
  • 34

2 Answers2

4

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.

Eric
  • 15,494
  • 38
  • 61
1

As a workaround you may be able to do something like this. Many mock frameworks work by tracking calls to matchers and then trying to associate those with the argument list later, which means you can call them all separately then pass them in, and Mockito is none the wiser. It looks messy of course, and you lose the type inference so you have to explicitly tell it what type to match on.

import org.mockito.Matchers
val objUnderTest = mock[UnderTest]
val m1 = Matchers.eq(1)
val m2 = Matchers.any[Int]
verify(objUnderTest).f(m1)(m2)
Nick
  • 11,475
  • 1
  • 36
  • 47