Mocking extension function in Kotlin

后端 未结 4 1425
暗喜
暗喜 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:30

    I use mockk library.

    For extension file write java name, like this:

    @file:JvmName(name = "ExtensionUtils")
    
    package myproject.extension
    
    ...
    

    And for fast codding I created file with different extension mocks:

    object FastMock {
    
        fun extension() = mockkStatic("myproject.extension.ExtensionUtils")
    
        fun listExtension() = mockkStatic("myproject.extension.ListExtensionUtils")
    
    }
    

    In test call this:

    FastMock.listExtension()
    every { itemList.move(from, to) } returns Unit
    

提交回复
热议问题