Java Android Instrumentation测试:无Instrumentation注册错误
我正在尝试使用浓缩咖啡编写Android仪器化测试。当我运行测试时,我得到了这个错误 java.lang.IllegalStateException:未注册任何检测!必须在注册检测下运行 我不明白这个错误是什么意思 我将简要说明我在测试中所做的工作。我遵循谷歌和谷歌给出的Java Android Instrumentation测试:无Instrumentation注册错误,java,android,android-studio,android-espresso,Java,Android,Android Studio,Android Espresso,我正在尝试使用浓缩咖啡编写Android仪器化测试。当我运行测试时,我得到了这个错误 java.lang.IllegalStateException:未注册任何检测!必须在注册检测下运行 我不明白这个错误是什么意思 我将简要说明我在测试中所做的工作。我遵循谷歌和谷歌给出的 @SmallTest @RunWith(AndroidJUnit4.class) 公共课堂活动输入测试{ @统治 public ActivityTestRule aiRule=新的ActivityTestRule(Activi
@SmallTest
@RunWith(AndroidJUnit4.class)
公共课堂活动输入测试{
@统治
public ActivityTestRule aiRule=新的ActivityTestRule(ActivityInput.class);
@以前
public void setUp(){}
}
当我运行测试时,错误发生在注释
@Rule
。我能解释一下为什么会发生这种情况,或者这背后有什么问题吗?我对使用代码不感兴趣,但对问题背后的实际问题更感兴趣。我也遇到了与您完全相同的错误,因为我使用了androidx
库。我建议您检查build.gradle中的库,并更改UI测试的依赖项,如以下代码块:
androidTestImplementation "com.android.support.test:runner:1.0.2"
androidTestImplementation "com.android.support.test:rules:1.0.2"
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
使用
com.android.support.test
或androidx
库选择其中一个,因为如果我们一起使用它们,它们将相互冲突。希望这能帮助您解决错误No instrumentation registed!必须在注册检测下运行。
更改从以下位置检索上下文的方式:
@Before
public void setUp() {
context = ApplicationProvider.getApplicationContext();
}
以不推荐的方式:
@Before
public void setUp() {
context = RuntimeEnvironment.application;
}
为我解决了这个问题。这不是一个解决方案。它只是通过删除此异常的实际生成器来消除异常
Androidx
testorchestration。我需要使用Test orchestrator进行浓缩咖啡测试,无法使用常规的com.android.support.Test:runner:1.0.2
您必须将所有库移动到androidx
,因为两个库相互冲突。只需选择其中一个,如果您想在Androidx中使用库,请将com.android.support.test:runner
转换为Androidx.test:runner:1.1.0
查看中的完整文档
@Before
public void setUp() {
context = RuntimeEnvironment.application;
}