You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Kotlin-Gradle单元测试失败:TimeMark类/接口不兼容问题

Kotlin-Gradle单元测试报错:java.lang.IncompatibleClassChangeError 解决思路

问题详情

单元测试执行时出现以下报错:

java.lang.IncompatibleClassChangeError: Found interface kotlin.time.TimeMark, but class was expected
    at io.kotest.engine.spec.interceptor.SpecFinishedInterceptor.intercept-0E7RQCE(SpecFinishedInterceptor.kt:37)

已尝试的操作:

  • 将Kotest版本从5.3.0升级至5.5.0
  • 升级Kotlin JVM版本(当前为1.6.21),问题未解决

解决步骤

  • 排查依赖冲突
    执行Gradle命令查看kotlin-time相关依赖的版本来源,定位冲突模块:

    ./gradlew dependencyInsight --dependency kotlin-time
    

    若发现旧版本的kotlin-time或kotlin-stdlib被第三方依赖引入,在对应依赖项中排除冲突模块,示例:

    implementation("some-third-party-lib") {
        exclude group: "org.jetbrains.kotlin", module: "kotlin-stdlib"
    }
    
  • 统一Kotlin与Kotest版本兼容性
    Kotest 5.5.0要求的最低Kotlin版本为1.7.x,kotlin.time.TimeMark在Kotlin 1.7中从类改为接口,当前1.6.21版本存在API不兼容问题。建议将Kotlin JVM版本升级至1.7.20或更高(如1.8.x),同时确保所有Kotest相关依赖(如kotest-runner-junit5、kotest-assertions-core)均使用5.5.0版本,避免混合不同版本的Kotest依赖。

  • 清理Gradle缓存并重新构建
    旧缓存可能残留不兼容的依赖文件,执行以下命令彻底清理后重新构建:

    ./gradlew clean build --refresh-dependencies
    
  • 强制统一项目Kotlin版本
    部分第三方库可能硬编码旧版Kotlin依赖,可在Gradle配置中强制所有依赖使用项目指定的Kotlin版本:

    subprojects {
        configurations.all {
            resolutionStrategy.eachDependency {
                if (requested.group == "org.jetbrains.kotlin") {
                    useVersion("1.8.22") // 替换为你要使用的Kotlin版本
                }
            }
        }
    }
    

内容的提问来源于stack exchange,提问作者rahul.cs

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.31 22:50:46