Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@ch200203
Copy link
Contributor

Fixes #1030 : "Throwing a descriptive error upon encountering inline function"

Kotlin inline functions are inlined at the call site, meaning their actual method calls disappear at runtime.
As a result, proxy-based mocking is fundamentally impossible.

Previously, attempts to mock inline functions would fail with vague messages like:
"Failed matching mocking signature..." or "Missing mocked calls...", making it difficult to debug.

This change classifies such failures into two clear categories and provides more actionable feedback:

  1. Interceptable environment (proxy created)
    • Detects inline functions in JvmMockFactoryHelper and immediately throws a MockKException
    • Message: Mocking Kotlin inline functions is not supported
  2. Non-interceptable environment (no proxy created)
    • When no calls are recorded, the DSL layer now includes a hint about possible inline function usage

ch200203 and others added 3 commits August 10, 2025 01:29
@Raibaz
Copy link
Collaborator

Raibaz commented Aug 11, 2025

Thanks for looking into this!

try {
val kotlinFunction = this.kotlinFunction
if (kotlinFunction != null && kotlinFunction.isInline) return true
} catch (_: Throwable) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are you catching Throwable here? Can't we be more specific and catch just the Exception types that we expect to be thrown?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the catch! πŸ‘
I agree that catching Throwable is too broad here. I’ll narrow it down to the concrete cases we actually expect when accessing kotlinFunction (e.g., KotlinReflectionInternalError and UnsupportedOperationException).
In those cases, I’ll fall back to the @kotlin.internal.InlineOnly annotation check.
I’ll push an update shortly!

}
}

private fun inlineHint(): String =
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why don't we inline (pun intended :P ) this instead of having it in a separate method, if it's used only once?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, fair point. πŸ˜„
I only pulled inlineHint() out so it’d be easier to tweak or remove later, but since it’s only used once, I’m fine with just inlining it.
I’ll make that change and push an update soon.

}

@Test
fun stubOfInlineFunctionViaReflectionThrowsFailFastError() {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice!

Copy link

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

πŸ“‹ Review Summary

This PR does a great job of improving the error messages when trying to mock Kotlin inline functions. The changes are clear and the new tests provide good coverage. I've added a couple of minor suggestions to improve code style and reduce duplication.

πŸ” General Feedback

  • The new error messages are much more descriptive and will be very helpful for users.
  • The addition of tests for the new behavior is excellent.
  • The approach to detect inline functions seems robust.

@Raibaz Raibaz merged commit 6f64cef into mockk:master Aug 12, 2025
24 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Throwing a descriptive error upon encountering inline function

2 participants