This question has two parts. First, you need to test what was the argument to a method invocation inside your class. Second, you want to do some form of function equality.
As mentioned in the comments, to solve the first problem you can use Mockito
. See the example below on how to do that using ArgumentCaptor
.
Now, the second part is more complex. See the answer here about function equality. In the code example below, I forced the functions func1
and func2
to be instantiated and stored them into val func_a
and val func_b
respectively. I then used the two val throughout my test code. If doing something similar is not possible during your test, then I am afraid there is NO good way of achieving what you need.
To better show the problem of function equality in Scala, I added the last two lines in the example.
import org.mockito.ArgumentCaptor
import org.mockito.Matchers._
import org.mockito.Mockito._
object ToyExample extends App {
// A toy class
class TargetClass {
def add(str: String, func: String => Long, j: Long): Long = func(str) + j
}
// These are the two functions we can use
def func1(g: String): Long = g.toLong
def func2(g: String): Long = g.toLong * 2
// Here is an example of using the TargetClass
val actualInstance = new TargetClass
println( actualInstance.add("12", ToyExample.func1, 2) ) // Prints 14
// Here is with the mock
val mockedSomeDao = mock(classOf[TargetClass])
val func_a = func1 _
val func_b = func2 _
// ... use the mocked object to do what you want
mockedSomeDao.add("12", func_a, 2)
// Now verify that the argument is the right one
val argument = ArgumentCaptor.forClass(classOf[(String) => Long])
verify(mockedSomeDao, atLeastOnce()).add(anyString(), argument.capture(), anyLong())
if( argument.getValue eq func_a ) {
println("Func 1") // This one gets called
} else if (argument.getValue eq func_b) {
println("Func 2")
}
println( func_a eq func_a) // Prints "true"
println( func1 _ eq func1 _) // Prints "false"!
}