运行Espresso测试时遇No instrumentation registered错误求助
解决Espresso测试中的"No instrumentation registered!"错误
报错信息
java.lang.IllegalStateException: No instrumentation registered! Must run under a registering instrumentation. at androidx.test.platform.app.InstrumentationRegistry.getInstrumentation(InstrumentationRegistry.java:45) at androidx.test.espresso.base.BaseLayerModule.provideTargetContext(BaseLayerModule.java:1) at androidx.test.espresso.base.BaseLayerModule_ProvideTargetContextFactory.provideTargetContext(BaseLayerModule_ProvideTargetContextFactory.java:1) at androidx.test.espresso.base.BaseLayerModule_ProvideTargetContextFactory.get(BaseLayerModule_ProvideTargetContextFactory.java:1) at androidx.test.espresso.base.BaseLayerModule_ProvideTargetContextFactory.get(BaseLayerModule_ProvideTargetContextFactory.java:2) at androidx.test.espresso.base.BaseLayerModule_ProvideDefaultFailureHanderFactory.get(BaseLayerModule_ProvideDefaultFailureHanderFactory.java:1) at androidx.test.espresso.base.BaseLayerModule_ProvideDefaultFailureHanderFactory.get(BaseLayerModule_ProvideDefaultFailureHanderFactory.java:2) at androidx.test.espresso.base.BaseLayerModule_ProvideFailureHanderFactory.get(BaseLayerModule_ProvideFailureHanderFactory.java:1) at androidx.test.espresso.base.BaseLayerModule_ProvideFailureHanderFactory.get(BaseLayerModule_ProvideFailureHanderFactory.java:2) at androidx.test.espresso.base.BaseLayerModule_FailureHandlerHolder_Factory.get(BaseLayerModule_FailureHandlerHolder_Factory.java:1) at androidx.test.espresso.base.BaseLayerModule_FailureHandlerHolder_Factory.get(BaseLayerModule_FailureHandlerHolder_Factory.java:2) at androidx.test.espresso.core.internal.deps.dagger.internal.DoubleCheck.get(DoubleCheck.java:6) at androidx.test.espresso.DaggerBaseLayerComponent.failureHandler(DaggerBaseLayerComponent.java:1) at androidx.test.espresso.DaggerBaseLayerComponent$ViewInteractionComponentImpl.viewInteraction(DaggerBaseLayerComponent.java:1) at androidx.test.espresso.Espresso.onView(Espresso.java:1) at com.example.espressotest.FirstTest.test1ChatId(FirstTest.java:51) at java.lang.reflect.Method.invoke(Native Method) at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47) at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17) at android.support.test.internal.runner.junit4.statement.RunBefores.evaluate(RunBefores.java:80) at android.support.test.internal.runner.junit4.statement.RunAfters.evaluate(RunAfters.java:61) at android.support.test.rule.ActivityTestRule$ActivityStatement.evaluate(ActivityTestRule.java:527) at org.junit.rules.RunRules.evaluate(RunRules.java:20) at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57) at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268) at org.junit.runners.ParentRunner.run(ParentRunner.java:363) at android.support.test.runner.AndroidJUnit4.run(AndroidJUnit4.java:101) at org.junit.runners.Suite.runChild(Suite.java:128) at org.junit.runners.Suite.runChild(Suite.java:27) at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268) at org.junit.runners.ParentRunner.run(ParentRunner.java:363) at org.junit.runner.JUnitCore.run(JUnitCore.java:137) at org.junit.runner.JUnitCore.run(JUnitCore.java:115) at android.support.test.internal.runner.TestExecutor.execute(TestExecutor.java:56) at android.support.test.runner.AndroidJUnitRunner.onStart(AndroidJUnitRunner.java:384) at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:2189)
当前build.gradle依赖配置
dependencies { implementation 'androidx.appcompat:appcompat:1.3.0' implementation 'com.google.android.material:material:1.3.0' implementation 'androidx.constraintlayout:constraintlayout:2.0.1' implementation 'androidx.navigation:navigation-fragment:2.3.0' implementation 'androidx.navigation:navigation-ui:2.3.0' testImplementation 'junit:junit:4.12' androidTestImplementation 'androidx.test.ext:junit:1.1.2' androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0' androidTestImplementation 'com.android.support.test:runner:1.0.2' androidTestImplementation 'com.android.support.test:rules:1.0.2' testImplementation 'org.robolectric:robolectric:4.0.2' testImplementation 'androidx.test:core-ktx:1.3.0' testImplementation 'androidx.test.ext:junit-ktx:1.1.2' androidTestCompile 'com.android.support:support-annotations:24.0.0' androidTestCompile 'com.android.support.test:runner:0.5' androidTestImplementation "com.android.support:support-annotations:27.1.1" androidTestImplementation 'com.android.support.test:rules:1.0.2' android { defaultConfig { testInstrumentationRunner 'android.support.test.runner.AndroidJUnitRunner' } testOptions { unitTests.includeAndroidResources = true } } }
修复步骤
1. 修正build.gradle结构错误
android块被错误地放在了dependencies内部,必须将其移到dependencies块外作为顶层配置块。
2. 统一依赖版本与库类型
当前配置混合了AndroidX和旧版Support库的测试依赖,会引发兼容性问题,需全部替换为AndroidX版本:
- 移除所有
com.android.support.test开头的依赖 - 使用AndroidX的
androidx.test:runner和androidx.test:rules替代 - 移除重复的
support-annotations依赖,AndroidX项目无需单独引入
3. 更新测试Instrumentation Runner
将testInstrumentationRunner改为AndroidX版本:androidx.test.runner.AndroidJUnitRunner
4. 清理重复依赖
移除所有重复声明的依赖(如重复的runner、rules),避免版本冲突。
修正后的build.gradle配置示例
android { defaultConfig { testInstrumentationRunner 'androidx.test.runner.AndroidJUnitRunner' } testOptions { unitTests.includeAndroidResources = true } } dependencies { implementation 'androidx.appcompat:appcompat:1.3.0' implementation 'com.google.android.material:material:1.3.0' implementation 'androidx.constraintlayout:constraintlayout:2.0.1' implementation 'androidx.navigation:navigation-fragment:2.3.0' implementation 'androidx.navigation:navigation-ui:2.3.0' // 本地单元测试依赖 testImplementation 'junit:junit:4.12' testImplementation 'org.robolectric:robolectric:4.0.2' testImplementation 'androidx.test:core-ktx:1.3.0' testImplementation 'androidx.test.ext:junit-ktx:1.1.2' // 仪器化测试依赖(Espresso) androidTestImplementation 'androidx.test.ext:junit:1.1.2' androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0' androidTestImplementation 'androidx.test:runner:1.4.0' androidTestImplementation 'androidx.test:rules:1.4.0' }
额外验证步骤
- 执行
File > Sync Project with Gradle Files同步配置 - 清理项目:
Build > Clean Project - 重新运行Espresso测试,确保选择Android Instrumented Tests而非本地单元测试
内容的提问来源于stack exchange,提问作者Priya
相关产品推荐
相关产品推荐

