Mocking a function with pass-by-name arguments

2019-08-15 06:46发布

问题:

My actual use-case is unit testing code involving finagle FuturePool: I want to make sure, FuturePool.apply was actually invoked, so that the task was executed in the correct instance of the pool.

The problem I am running into however seems more generic, so I will illustrate it on an abstract example, not related to finagle or futures.

Suppose, I have these two classes:

    class Foo {
      def apply(f: => String) = f
    }

    class Bar(val foo: Foo) {
      def doit(f: => String) = foo(f)
    }

Bar has an instance of Foo, that knows how to run functions, and I want to test that it is actually using it for execution:

    describe("Bar") {
      it("should use the right foo") {
        val foo = mock[Foo]
        when(foo.apply(any)).thenAnswer( new Answer[String] {
          def answer(invocation: InvocationOnMock): String =
            invocation.getArgumentAt(0, classOf[Function0[String]]).apply()
        })
        new Bar(foo).doit("foo") should equal("foo")
      }
    }

This does not work: .doit return null, apparently, because mockito does not realize it was mocked. It seems that any is not matching Function0 in this case (replacing it with any[Function0[String]] does not help either.

I also tried it another way:

it("should Foo!") {
    val foo = Mockito.spy(new Foo)
    new Bar(foo).doit("foo") should equal("foo")
    verify(foo).apply(any)
  }

This also does not work, and kinda confirms my suspicion about any not working in this case:

Argument(s) are different! Wanted:
foo$1.apply(
    ($anonfun$apply$mcV$sp$7) <function0>
);
Actual invocation has different arguments:
foo$1.apply(
    ($anonfun$apply$mcV$sp$6) <function0>
);

Any ideas about a good way to get around this?

回答1:

This signature:

def apply(f: => String)

is known as "call by name" where it passes an expression instead of an evaluated expression. This is very specific to Scala and not well supported with Mockito.

There is a host of workarounds to this:

  • Is there a way to match on a call-by-name argument of a Mockito mock object in Specs?

  • How to mock a method with functional arguments in Scala?

  • How do you mock scala call-by name in Mockito

The one by Eric looks the simplest and what you may want.