Hello guys, we have a little problem with mocking in out unit tests. It looks a bit like an error pe...
c

Christoph Beylage

over 3 years ago
Hello guys, we have a little problem with mocking in out unit tests. It looks a bit like an error per design in kotlin. but actually, i cant believe this ๐Ÿ˜„ we tried mockk and mockito regarding this and both result in the same problem. imagine we have an instance of the following class:
data class AppConfig(
    var smtp = SmtpConfig(
            enabled = false
            host = "test",
            port = 12345,
            useStartTLS = true,
            fromName = "test",
            from = "<mailto:test@test.de|test@test.de>"
            user = ""
            password = ""
    )
)

val classInstance = AppConfig()
what I want to do in one of our tests is to mock the value of smtp. What I did - here for example with mockito - was the following:
val spy = Mockito.spy(classInstance)
Mockito.`when`(spy.smtp).thenReturn(SmtpConfig(true)). //true is the enabled flag in this case, so the rest should be null
What Mockito and Mockk are doing in this case is to mock the get method behind that property. This can be verified by starting the debugger right in the line after the Mockito.\`when\` expression. The field is exactly like initialized above and when I click the getter in the debugger I get the result from spy.smtp, as you can see in the screenshot. I can understand why this happens, but kotlin simply does not use the getters of those properties why the tests wont work. So long text, simple question: Any idea how to fix this? Everything I found was a github issue from mockk with exactly the same problem and that has been closed because to support this a major rebuild would be necessary.