Daniel
06/25/2018, 7:51 PMdelayer
) then I would use (this is a question):
argumentCaptor<() -> Unit>().apply {
whenever(delayer.delay(capture())).thenAnswer {
firstValue()
}
}
?
Now suppose I only want to invoke the lambda a defined number of times because the code calles delay(...)
recursively until the user stops it.
How would I do this?
I came up with:
argumentCaptor<() -> Unit>().apply {
var invocationCount = 0
whenever(delayer.delay(capture())).thenAnswer {
if(invocationCount < times) {
++invocationCount
firstValue()
}
}
}
But that does not seem like a clean solutionAndreas Sinz
06/25/2018, 8:38 PMthenAnswer
have?Daniel
06/25/2018, 8:44 PMpublic abstract OngoingStubbing<T> thenAnswer(org.mockito.stubbing.Answer<?> answer)
from mockito-coresnowe
06/25/2018, 10:43 PMmockito-kotlin
might have some solution for what you're encountering.Andreas Sinz
06/26/2018, 11:25 AMAnswer
and use that with a lambda, e.g.:
class CountedAnswer<T>(invocationCount: Int, f: () -> T) : Answer<T> {
private var counter = 0
override fun answer(InvocationOnMock invocation): T {
if(counter < invocationCount)
f()
....
}
//Usage
delayer.delay(capture()).thenAnswer(CountedAnswer { firstValue() })
Daniel
06/27/2018, 5:51 PMDaniel
06/27/2018, 5:51 PM