I need to mock a call to some class and make it take some time.
The current code uses this:
every { useCase.execute(any()) } answers {
AnswersWithDelay(50000, DoesNothing.doesNothing())
}
Now I am changing execute()
to return an object of Notification
class.
val notif = Notification(...)
But I can't figure out how to change this mock.
val answer: org.mockito.stubbing.Answer<Notification> = AdditionalAnswers.answer { invocation: InvocationOnMock -> notif }
val delayedAnswer = AdditionalAnswers.answersWithDelay(50000, { invocation: InvocationOnMock -> answer } )
I can't find how to make the answers { ... }
compilable. Any tips?
The Mockito and MockK APIs are a bit confusing, because they share the terminology, but are not compatible. MockK's io.mockk.Answer
is not compatible with Mockito's org.mockito.stubbing.Answer
, and from there, all the other util classes do not match either.
So while originally it could have used DoesNothing.doesNothing()
, because the return type was Unit
/void
, with a return type the mock must be created with MockK's idiomatic way.
So I resorted to:
val answerF = FunctionAnswer { Thread.sleep(50000); notif }
every { useCase.execute(any()) } .answers(answerF)
As a takeaway, for Kotlin projects, I would recommend removing Mockito if possible.