Mocking extension function in Kotlin

后端 未结 4 1423
暗喜
暗喜 2021-02-19 01:19

How to mock Kotlin extension function using Mockito or PowerMock in tests? Since they are resolved statically should they be tested as static method calls or as non static?

4条回答
  •  野的像风
    2021-02-19 01:50

    First of all, Mockito knows nothing Kotlin specific language constructs. In the end, Mockito will have a look into the byte code. Mockito is only able to understand what it finds there and what looks like a Java language construct.

    Meaning: to be really sure, you might want to use javap to deassemble the compiled classfiles to identify the exact names/signatures of the methods you want to mock.

    And obviously: when that method is static, you have to user PowerMock, or JMockit; if not, you should prefer to with Mockito.

    From a java point of view, you simply avoid mocking static stuff; but of course, things get really interesting, now that different languages with different ideas/concepts come together.

提交回复
热议问题