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

Android Espresso运行UI测试报FileTestStorage类未找到错误如何解决

错误原因
  • 测试依赖版本不匹配:你引入了androidx.test.espresso:espresso-core:3.5.0-alpha03这个alpha版本的Espresso依赖,它依赖1.5.0及以上版本的androidx.test:core库,FileTestStorage这个类是1.5.0以上版本的测试核心库才新增的,但你项目里其他测试核心依赖都是1.4.0稳定版,版本不匹配导致类找不到。
  • 依赖冗余混乱:你的dependencies里重复声明了多次espresso-core、androidx.test:runner、androidx.test:rules依赖,版本还互不相同,同时还残留了废弃的com.android.support.test系列的旧支持库测试依赖,新旧依赖、同库不同版本的依赖冲突也会导致类加载异常。
解决步骤
  1. 首先清理dependencies中冗余、冲突、过时的测试依赖,删除以下所有内容:
androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.0-alpha03'
androidTestImplementation "com.android.support:support-annotations:28.0.0"
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test:rules:1.0.2'
androidTestImplementation 'androidx.test:rules:1.4.1-alpha03'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
androidTestImplementation 'androidx.test:runner:1.4.0'
androidTestImplementation 'androidx.test:rules:1.4.0'
androidTestImplementation "androidx.test:runner:1.4.0"
androidTestImplementation "androidx.test:rules:1.4.0"
androidTestImplementation "androidx.test.espresso:espresso-core:3.4.0"
  1. 统一添加版本一致的稳定版AndroidX测试依赖,在dependencies中补充如下内容:
// 统一使用稳定版测试依赖
androidTestImplementation 'androidx.test:core:1.5.0'
androidTestImplementation 'androidx.test:runner:1.5.2'
androidTestImplementation 'androidx.test:rules:1.5.0'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
  1. 可选优化:你当前使用的ActivityTestRule已经废弃,建议替换成ActivityScenario,无需额外声明Rule代码,测试写法更简洁:
@LargeTest
@RunWith(AndroidJUnit4::class)
class MainActivityTest {

    @Test
    fun uitest_dialogIsShown() {
        // 启动MainActivity
        val scenario = ActivityScenario.launch(MainActivity::class.java)
        
        onView(withId(R.id.floatingActionButton)).perform(click())

        onView(withText("Please input an appropriate span count value between 1 and 100:"))
            .inRoot(isDialog())
            .check(matches(isDisplayed()))
        
        // 测试结束关闭场景
        scenario.close()
    }
}
  1. 清理项目缓存:执行Android Studio的File -> Invalidate Caches...,勾选清除文件系统缓存和本地历史,重启后重新构建项目再运行测试即可。

内容的提问来源于stack exchange,提问作者thebluepandabear

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.25 10:45:09