https://kotlinlang.org logo
Title
d

dave08

06/10/2018, 5:37 PM
How do I mock
resolve
extension from Kotlin stdlib on a File? I tried this, but it still seems to call the Kotlin code:
fun mockFile() = mockk<File>().apply {
	every { resolve(any<File>()) } answers { mockk<File>(relaxed = true).apply {
		every { path } returns "this.txt"
	} }
}

// Gives
path must not be null
java.lang.IllegalStateException: path must not be null
	at kotlin.io.FilesKt__FilePathComponentsKt.isRooted(FilePathComponents.kt:81)
	at kotlin.io.FilesKt__UtilsKt.resolve(Utils.kt:400)
g

ghedeon

06/10/2018, 5:38 PM
o

oleksiyp

06/10/2018, 9:10 PM
With extension functions it can be tricky. Always need to understand what calls are happening on byte code level.
mockkStatic("kotlin.io.FilesKt__UtilsKt")

        val file = File("abc")
        every {
            file.resolve("def")
        } returns File("ghi")

        println(File("abc").resolve("def"))