https://kotlinlang.org logo
#mockk
Title
# mockk
k

kyleg

12/25/2019, 8:10 PM
Aha. If I have top-level
foo: (File)->Long
I want to mock, and top-level (same file)
caller: (foo: (File)->Long)->Int
that I do not want to mock, then that file needs
@file:JvmName("FooKt") ; package <http://path.to|path.to>
up top, and then in the test:
Copy code
with(::caller) {
            mockkStatic("path.to.FooKt")
            every { foo(any()) } returns 0L

            this(::foo)

            verify { foo(File("/path/to/thing") }
        }
k

Karolis

12/30/2019, 9:52 AM
@JvmName
is not neccessary if your file is called
Foo.kt
since kotlin compiler will generate a class called
xKt
for a file named
x.kt
.
k

kyleg

12/30/2019, 7:43 PM
@Karolisthe MockK docs say otherwise for top-level functions: https://blog.kotlin-academy.com/mocking-is-not-rocket-science-mockk-advanced-features-42277e5983b5 (under the heading for Top-Level Functions:
To know exactly where such function as [top-level function]
lowercase
will land you need to check actual class files produced by the build.
Sometimes names are fancy. For example:
mockkStatic(“kotlin.io.FilesKt__UtilsKt”)
every { File(“abc”).endsWith(any<String>()) } returns true
In Kotlin there is a special directive to guide compiler what file to put such top-level functions. It is called
@file:JvmName
[snip]
Seems like while what you say is usually true, it is not true all the time. “Sometimes names are fancy”
k

Karolis

12/30/2019, 7:44 PM
TIL, thanks! Didn't know about those fancy names.
k

kyleg

12/30/2019, 7:45 PM
No problem. I’m just lucky I found those docs. I got it working, but hit a wall, refactored, then realized I could have spied instead of mocked. Oh well, you live, you learn 🙂
3 Views