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系列的旧支持库测试依赖,新旧依赖、同库不同版本的依赖冲突也会导致类加载异常。
解决步骤
- 首先清理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"
- 统一添加版本一致的稳定版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'
- 可选优化:你当前使用的
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() } }
- 清理项目缓存:执行Android Studio的
File -> Invalidate Caches...,勾选清除文件系统缓存和本地历史,重启后重新构建项目再运行测试即可。
内容的提问来源于stack exchange,提问作者thebluepandabear
相关产品推荐
相关产品推荐

