mockito / mockito-kotlin

Using Mockito with Kotlin
MIT License
3.1k stars 200 forks source link

Please make clear that methods and classes must BOTH be open to mock. #389

Open sleekweasel opened 3 years ago

sleekweasel commented 3 years ago

Please highlight in your introductory text that both methods and classes need to be marked open - perhaps by way of example.

I wasted a lot of time chasing around why

open class TimeFactoryX {
    fun now() : Instant = Instant.now()
}

class TimeFactoryTest {
    @Test
    fun now() {
        val f = mock<TimeFactoryX>() {
            on { now() }.doReturn(Instant.now())
        }
    }
}

caused this response

when() requires an argument which has to be 'a method call on a mock'.
For example:
    when(mock.getArticles()).thenReturn(articles);

Also, this error might show up because:
1. you stub either of: final/private/equals()/hashCode() methods.
   Those methods *cannot* be stubbed/verified.
   Mocking methods declared on non-public parent classes is not supported.

because while I remembered that classes are final by default under kotlin, I don't recall knowing that methods are too, by default.

This doesn't seem to be required by mockk, so despite its worse diagnostics, I'm probably going to shift to that.

tadfisher commented 3 years ago

@sleekweasel Is the kotlin-allopen compiler plugin not an option for you?

sleekweasel commented 3 years ago

@tadfisher I'm not asking that mockito-kotlin changes, just that its documentation be made clearer. Mentioning that such a plug-in exists would be a good part of that improvement in documentation.

bohsen commented 3 years ago

Go for Mockk for whatever reason, but please realise before writing issues like this, that this is an open source project developed mostly by one person (probably on his spare time). Also everyone can create a PR for open source projects.

It's just unfair to assume that maintainers have nothing else to do than maintain their open source projects.

I find issues like this so demeaning. Sorry if I've misjudged the intention. Just sayin'.