mercredi 3 février 2016

Mocking scala method with functional parameter using Mockito

Working with Finatra and trying to mock a database call for testing. I'm using finagle-postgres which defines this method:

def prepareAndQuery[T](sql: String, params: Any*)(f: Row => T): Future[Seq[T]] = {...}

Other mocks using Mockito have been straightforward, but I can't seem to get this one to work. After struggling with the compiler I finally found this, but it doesn't work at run-time:

case class Foo( a: Int, b: Int, c: Int )
val client = smartMock[Client]
def f(row:Row): Foo = {
  Foo(1,2,3)
}
client.prepareAndQuery[Foo]("select 1") returns { x => f(x) }

Executing the test throws:

org.mockito.exceptions.verification.SmartNullPointerException: 
...
because this method call was *not* stubbed correctly:

I'm new to Mockito and I haven't found an example of mocking a function that takes a function parameter. Any thoughts on how my mock is wrong?

Aucun commentaire:

Enregistrer un commentaire