9

I have this extension:

src/main/kotlin/com/myproject/api/extensions.kt

fun String.asJson() : JsonObject {
    return JsonObject.readFrom(this)
}

When I run my application, it works fine. But, when I run a test case that uses that extension function, it crashes:

java.lang.NoSuchMethodError: com.myproject.api.ExtensionsKt.asJson(Ljava/lang/String;)Lcom/eclipsesource/json/JsonObject;

What am I missing?

Héctor
  • 24,444
  • 35
  • 132
  • 243

1 Answers1

31

After hours, I finally found the error. I already had a extensions.kt file in the same package but in test directory! src/test/kotlin/com/myproject/api/extensions.kt. I changed the name to testExtensions.kt and everything is working now.

Héctor
  • 24,444
  • 35
  • 132
  • 243
  • 1
    2021 and Kotlin 1.5.30 here and this is still relevant. If you have the same file (case insensitive, even) in two different locations on the classpath (e.g. in a multiproject Gradle project), you get this weird exception -- `NoSuchMethodError`. – Max Sep 02 '21 at 23:42
  • 2023 in Kotlin 1.7.10 still relevant. I think a better error message is needed! – Simon Jacobs Jan 15 '23 at 21:25
  • 1
    Thank you. I have no idea that this could happen. Have no idea when looking at the error log :) – truongnm Feb 23 '23 at 10:53
  • https://youtrack.jetbrains.com/issue/KT-17206/NoSuchMethodError-in-runtime-with-same-named-files-and-packages-but-different-source-sets-modules – Dalibor Filus Apr 27 '23 at 18:07