mockito-kotlin icon indicating copy to clipboard operation
mockito-kotlin copied to clipboard

What about improving doReturn -> when implementation?

Open htafoyag opened this issue 3 years ago • 0 comments
trafficstars

I believe there's a missed opportunity on the doReturn.when implementation. Mockito kotlin is only changing reseved word 'when' with whenever, but the implementation is not on par with the rest of the library.

When using on { myMethod() } doReturn "something", myMethod is inferred from the mock scope. However the doReturn is not doing the same and we need to resend the mock to whenever method:

val myMock = mock<MyClass> {
    doReturn("something").whenever(myMock).myMethod()
}

A better format would be:

val myMock = mock<MyClass> {
    doReturn("something").whenever(myMethod())
}

or even a format similar to the ones already there:

val myMock = mock<MyClass> {
    doReturn "something" whenever { myMethod() }
}

The same for doNothing and other similar methods

Or by a fact, i would've preferred that on - doReturn called doReturn.when instead, I see no purpose on invoking the real methods and the need to use a different method for spies.

htafoyag avatar Feb 10 '22 20:02 htafoyag