使用Splash Screen API与Navigation时的Espresso仪器测试问题排查
问题
我在使用Splash Screen API(无需Activity,通过自定义主题实现)和Navigation组件(将HomeActivity设为defaultNavHost,交互逻辑在HomeFragment)时,编写Espresso仪器测试遇到了问题:
- 最初执行测试时出现
Execution failed for task ':app:processDebugAndroidTestManifest'错误 - 调整后又出现找不到
androidx.test:rules:1.5.2的依赖错误
测试代码如下:
@RunWith(AndroidJUnit4ClassRunner::class) class HomeActivityTest { @Before fun setUp() { ActivityScenario.launch(HomeActivity::class.java) } @Test fun performSwipeUpRecyclerViewContainerToTop() { onView(withId(R.id.rv_features)).check(matches(isDisplayed())) } }
新错误信息:
* What went wrong: Execution failed for task ':app:processDebugAndroidTestManifest'. > Could not resolve all files for configuration ':app:debugAndroidTestRuntimeClasspath'. > Could not find androidx.test:rules:1.5.2. Searched in the following locations: - https://dl.google.com/dl/android/maven2/androidx/test/rules/1.5.2/rules-1.5.2.pom - https://repo.maven.apache.org/maven2/androidx/test/rules/1.5.2/rules-1.5.2.pom - https://jcenter.bintray.com/androidx/test/rules/1.5.2/rules-1.5.2.pom Required by: project :app
解决方案
1. 修复依赖问题
androidx.test:rules:1.5.2是不存在的版本,AndroidX Test的rules库稳定版目前最高为1.5.0。你需要在模块级build.gradle中修正依赖配置:
// 替换成正确的AndroidX Test依赖版本 androidTestImplementation 'androidx.test.ext:junit:1.1.5' androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1' androidTestImplementation 'androidx.test:rules:1.5.0'
同时检查项目根目录的build.gradle,确保配置了Google Maven仓库(AndroidX依赖必须从此仓库拉取),并移除已停止维护的jcenter:
allprojects { repositories { google() mavenCentral() } }
2. 优化测试代码(可选)
因为交互逻辑在HomeFragment,直接启动Activity后可能需要等待页面渲染完成,避免断言失败。可以添加等待逻辑,或者直接测试Fragment(更聚焦):
@RunWith(AndroidJUnit4ClassRunner::class) class HomeActivityTest { private lateinit var scenario: ActivityScenario<HomeActivity> @Before fun setUp() { scenario = ActivityScenario.launch(HomeActivity::class.java) // 等待RecyclerView加载完成后再执行测试 onView(withId(R.id.rv_features)).waitForDisplayed() } @Test fun performSwipeUpRecyclerViewContainerToTop() { onView(withId(R.id.rv_features)).check(matches(isDisplayed())) // 如需测试滑动,可添加: // onView(withId(R.id.rv_features)).perform(swipeUp()) } // 自定义等待View显示的扩展函数 private fun ViewInteraction.waitForDisplayed(timeout: Long = 5000): ViewInteraction { val waitAction = object : ViewAction { override fun getConstraints(): Matcher<View> = isDisplayed() override fun getDescription(): String = "等待View显示" override fun perform(uiController: UiController, view: View) { uiController.loopMainThreadUntilIdle() val endTime = System.currentTimeMillis() + timeout while (System.currentTimeMillis() < endTime) { if (view.isDisplayed()) return uiController.loopMainThreadForAtLeast(50) } throw PerformException.Builder() .withActionDescription(description) .withCause(TimeoutException("超时:View未在${timeout}ms内显示")) .withViewDescription(HumanReadables.describe(view)) .build() } } return perform(waitAction) } }
3. 检查测试Manifest配置
最初的Manifest处理错误,可能是测试目录下的AndroidManifest.xml配置冲突。确保测试Manifest包含必要的配置:
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.your.package.test"> <application> <activity android:name=".HomeActivity" /> </application> </manifest>
内容的提问来源于stack exchange,提问作者dans
相关产品推荐
相关产品推荐

