I just found this blog post . It describes how to use implicit conversions to achieve what you want. If you define an implicit conversion like this
implicit def toAnswerWithArgs[T](f: InvocationOnMock => T) = new Answer[T] { override def answer(i: InvocationOnMock): T = f(i) }
you can call thenAnswer with a simple function as an argument:
when(mock.someMethodCall()).thenAnswer((i) => calculateReturnValue())
There is also a slightly shorter alternative version for the case where your mocking method has no arguments. See the link for more details.
lex82 source share