dimanche 3 juin 2018

How to do non specialized type has method that accepts Function1?

I have the following class:

class Elem[T](t: T){
  def doSome(f: T => Unit):Unit = f(t)
}

So I want to test function invokation for the given Elem instance (I'm doing with mockito). When running the test

val f = mock(classOf[Int => Unit])
new Elem(1).doSome(f)
verify(f).apply(1)

I got the following exception:

Wanted but not invoked:
function1.apply$mcVI$sp(1);

However, there was exactly 1 interaction with this mock:
function1.apply(1);

That's reasonable because Function1 is specialized for Int. So making the test as

val f = mock(classOf[AnyRef => Unit])
new Elem(1.asInstanceOf[AnyRef]).doSome(f)
verify(f).apply(1.asInstanceOf[AnyRef])

works fine.

Is there a way to avoid this ugly casts to AnyRef? Maybe there is another tool more suitable for this case than mockito?

Aucun commentaire:

Enregistrer un commentaire