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

更新依赖后Android Instrumented Tests无法正常运行求助

Instrumented Tests 无限构建且无结果(0/0)——依赖更新后异常排查

问题背景

更新以下依赖版本后,Instrumented Tests无法正常运行:

mockitoVersion = '5.3.0'
dexMakerVersion = '2.28.3'
espressoVersion = '3.5.1'

对应的测试依赖配置:

androidTestImplementation "org.mockito:mockito-core:$mockitoVersion"
androidTestImplementation "com.linkedin.dexmaker:dexmaker-mockito:$dexMakerVersion"
androidTestImplementation "androidx.test.espresso:espresso-contrib:$espressoVersion"

现象:点击运行后Gradle无限构建,测试结果始终显示0/0;单元测试可正常执行,仅Instrumented Tests异常。

已尝试的操作:

  • 更新Android Studio至Flamingo | 2022.2.1
  • 更新Gradle至8.0.0
  • 多次清除缓存并重启
  • 清理并重建项目
  • 更新库
  • 配置了testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"

可能的解决方法

1. 修复DexMaker与Mockito的版本兼容性

Mockito 5.x对DexMaker版本有明确要求,当前dexmaker-mockito:2.28.3与Mockito 5.3.0不兼容。尝试升级DexMaker到适配版本:

dexMakerVersion = '2.31.0'

同步后重新运行测试。

2. 替换为Android专用Mockito依赖

mockito-core是通用Java版本,Android Instrumented Tests建议使用mockito-android,它内置了Android适配逻辑,可避免与DexMaker冲突:

// 替换原mockito-core依赖
androidTestImplementation "org.mockito:mockito-android:$mockitoVersion"
// 保留dexmaker-mockito(确保版本兼容)
androidTestImplementation "com.linkedin.dexmaker:dexmaker-mockito:$dexMakerVersion"

3. 排查Espresso-Contrib的依赖冲突

Espresso-contrib容易引入库冲突(如AndroidX或Support库版本不一致),可尝试排除冲突模块,或暂时移除该依赖验证问题:

androidTestImplementation "androidx.test.espresso:espresso-contrib:$espressoVersion" {
    exclude group: 'androidx.appcompat', module: 'appcompat'
    exclude group: 'androidx.recyclerview', module: 'recyclerview'
    exclude group: 'androidx.cardview', module: 'cardview'
}

4. 检查测试类注解配置

确保所有Instrumented Test类都正确使用注解:

import androidx.test.ext.junit.runners.AndroidJUnit4;
import org.junit.Test;
import org.junit.runner.RunWith;

@RunWith(AndroidJUnit4.class)
public class ExampleInstrumentedTest {
    @Test
    public void testSample() {
        // 测试逻辑
    }
}

若使用自定义Test Runner,需确认它继承自AndroidJUnitRunner且配置正确。

5. 查看Gradle详细构建日志

在Android Studio的Gradle Console中切换到info或debug级别,查看构建过程中是否有隐藏错误(如依赖下载超时、DEX合并失败等),定位阻塞点。

6. 降级Mockito到兼容版本

若上述方法无效,可暂时降级Mockito到4.x稳定版本,保持与DexMaker的兼容性:

mockitoVersion = '4.11.0'
dexMakerVersion = '2.28.3'

测试恢复正常后,再逐步尝试升级版本。


内容的提问来源于stack exchange,提问作者Xena Warrior Princess

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.23 04:27:19