-
Notifications
You must be signed in to change notification settings - Fork 206
Closed
Description
Hello,
I've been independently working on getting kotlin working with Mockito, and have done very little work in comparison to your work.
One thing I have got that I think your repo could use however is an idiom around setting up stubbed and mocked methods:
val fileTracker = mock<LastModifiedTimeTracker> {
on { fileWasModified() }.thenReturn(true)
}this is implemented with the relatively simple extension methods:
inline fun <reified T : Any> mock(stubbing: KStubbing<T>.() -> Unit) : T
= Mockito.mock(T::class.java).apply { stubbing.invoke(KStubbing(this)) }
class KStubbing<TMock>(private val mock: TMock) {
fun <TMethodReturn> on(methodCall: TMethodReturn) = Mockito.`when`(methodCall)
fun <TMethodReturn> on(methodCall: TMock.() -> TMethodReturn) = Mockito.`when`(mock.methodCall());
}i have not done extensive testing, but in thinking about it, the call order that mockito needs so badly should be preserved.
- on the first line, a mock is created.
- using that mock as a return value, the apply method is called
stubbing.invokeis effectivelywhen(methodCall)- the specifics of both the
onmethods will have the arguments executed first, meaningaMock.call()will happen beforeMockito.when, as is expected.
another win here is that unit is a type that is a subtype of Object, so no more doAnswer(...).when nonsense :)
I can put this into a pull request and add some tests if you like.
Happy mocking!
nhaarman
Metadata
Metadata
Assignees
Labels
No labels