skip to content

When you use MockK's mockkStatic to stub a Kotlin top-level or extension function, how do you work out the exact string to pass, and what can change that name?

level: middleimportance: must knowfreq 45%

answer

  1. package + FileName + Kt
  2. @file:JvmName rewrites the facade
  3. directory irrelevant, package matters
  4. string is not compiler-checked
  5. KClass overload for real classes

basics

~20 s

Pass the JVM name of the file facade class: package plus source file name with Kt appended. Greetings.kt in com.acme becomes com.acme.GreetingsKt. A @file:JvmName annotation on that file replaces the name, and the string must match exactly.

solid answer

~50 s

Kotlin has no free functions on the JVM, so the compiler puts every top-level function of a source file (including top-level extension functions) into a synthetic class called the **file facade**. Its name is `<package>.<FileName>Kt`: `Greetings.kt` in package `com.acme` compiles to `com.acme.GreetingsKt`. That is the string `mockkStatic("com.acme.GreetingsKt")` expects. The `KClass` overload is for real classes (Java statics, members) — there is no `KClass` you can write for a facade in source. Two things move the name. `@file:JvmName("Greetings")` renames the facade to `com.acme.Greetings` with no `Kt` suffix. `@file:JvmMultifileClass` merges several files into one facade under that shared name, with per-file implementation classes behind it. Directory layout is irrelevant — only the declared package and the file name matter. The string is not compiler-checked, so renaming or moving the file breaks the test at runtime rather than at build time. When unsure, read the class name out of the compiled output.

code

kotlin · 5 lines
kotlin
package com.acme

fun greet(name: String) = "hello $name"

fun String.shout() = uppercase()

go deeper

for a junior

Be able to state the rule — package plus file name plus Kt — and show one working mockkStatic call.

for a middle

Explain why the facade exists at all and name the two annotations that change it, plus how you would verify the name from the build output.

for a senior

Add the maintenance angle: the string is unchecked, so file renames break tests silently; discuss pinning the name or avoiding the seam.

for a principal

Frame it as an API-shape decision — code you intend to be mockable statically should have a deliberate, stable JVM name, or better, an injectable boundary.

## Why a string at all On the JVM every method lives in a class. Kotlin lets you declare functions at the top level of a file, so the compiler manufactures a class to hold them — the **file facade** — and the functions become static methods on it. MockK's static mocking works by instrumenting a *class*, so to intercept a top-level function you must name the facade that holds it. Since that class has no name you can write in Kotlin source (you cannot say `GreetingsKt::class`), MockK offers a `String` overload: `mockkStatic("com.acme.GreetingsKt")`. ## The naming rule The default facade name is the **declared package**, a dot, the **source file name** with the extension dropped, first letter capitalised, and the suffix `Kt`: - `com/acme/Greetings.kt`, `package com.acme` -> `com.acme.GreetingsKt` - `util/string-utils.kt` is not legal Kotlin naming for this purpose in practice; a file `StringUtils.kt` -> `...StringUtilsKt` - a file in the default package -> `GreetingsKt` with no prefix The **directory** does not participate; the `package` declaration does. A file moved between source sets or directories without changing its package keeps the same facade name. ## What changes the name `@file:JvmName("Greetings")` at the very top of the file (before the `package` line) replaces the facade name entirely: the class becomes `com.acme.Greetings` — no `Kt`. Libraries do this routinely for Java-friendly APIs, and it is the single most common reason a correct-looking `mockkStatic` string fails. `@file:JvmMultifileClass` (used together with `@file:JvmName`) merges the top-level declarations of several files into one facade with that name. The implementation is split into internal per-file classes behind the facade, which makes such functions awkward to instrument — treat a multifile facade as a signal to find a different seam rather than a naming puzzle to solve. ## The other overloads `mockkStatic(SomeJavaClass::class)` takes a `KClass` and is the right call for Java `static` methods or for members of a real class. It is the same mechanism — instrument a named class — expressed with a compiler-checked reference. Prefer it whenever a real class exists, precisely because a typo becomes a compile error instead of a runtime surprise. ## Failure mode and how to check The string is opaque to the compiler. Rename `Greetings.kt` to `GreetingText.kt` in a refactor and nothing in the IDE flags the test; it fails only when it runs. There is no partial matching and no wildcard: the name must be exactly the binary class name. To confirm a name, look at the compiled output — the `.class` files under your build directory carry the facade name directly (`build/classes/kotlin/main/com/acme/GreetingsKt.class`), or inspect the class with a bytecode viewer. Doing this once when you write the test is cheaper than debugging a silent miss later. ## Practical consequence Because the binding is by string, static mocking of top-level functions is inherently brittle against file renames. Teams that use it a lot tend to keep the mocked functions in a small, stable, deliberately named file — sometimes with an explicit `@file:JvmName` — so the facade name is a decision rather than an accident.

  • Why is there no KClass overload you can use for a top-level function?
    The facade class is synthesised by the compiler and is not addressable from Kotlin source, so there is no `SomethingKt::class` literal to write. MockK therefore accepts the binary class name as a string and resolves it reflectively. For Java statics and members of real classes the `KClass` overload exists and should be preferred, because the compiler checks it.
  • A colleague renames the file holding a mocked top-level function. What happens to the test and how would you make that safer?
    Nothing fails at compile time; the test fails when it runs, because the named facade no longer exists or no longer holds that function. Safer options are to pin the facade name with an explicit `@file:JvmName`, to keep such functions in one small stable file, or to remove the need for static mocking by routing the call through an injected interface.

  • Passing the source file path or the package alone instead of the binary class name
  • Assuming the directory the file sits in is part of the facade name
  • Believing a typo in the string will be caught by the compiler or fail loudly at the right place
  • Thinking @file:JvmName has no effect on what mockkStatic needs
  • Trying to write GreetingsKt::class in Kotlin source

context