mockito-kotlin
mockito-kotlin copied to clipboard
Infix extension function for doReturn stub construction
Mockito provides 2 constructions for stubbing:
doReturn(...).when(...)
when(...).thenReturn(...)
The first one is known not to call real method, which is sometimes handy (source)
There are nice extensions for the second use case, which allow for more concise writing of stubbing:
search.stub {
on { what } doReturn WHAT
}
It would be nice to provide consise construction for the first use case:
search.stub {
doReturn { WHAT } on { what}
}
Those examples are of course very basic, but it would become more helpful in more advanced use cases.
Basically what is needed is providing method doReturn function in KStubbing interface and infix extension function called on to OngoingStubbing interface, I guess.
I hope you see my point
You can use lambda with doAnswer. Is it not enough?
search.stub {
on { what } doAnswer { WHAT }
}