无法解析符号InstantTaskExecutorRule

82

我打开了示例代码BasicRxJavaSample(来自于这篇文章Room+RxJava)主要内容如下:

@Rule
public InstantTaskExecutorRule instantTaskExecutorRule = 
    new InstantTaskExecutorRule();

而BasicRxJavaSample是正常的。 但我无法在我的测试中应用它。 发生了什么:

无法解析符号InstantTaskExecutorRule

enter image description here

手动导入也不起作用:

enter image description here

我的自动完成是这样工作的 enter image description here

但应该是这样的

enter image description here

我的app build.gradle (完整gradle请点击此处):

// tests
testImplementation 'junit:junit:4.12'
androidTestCompile "com.android.support:support-annotations:$supportVersion"
testImplementation "android.arch.core:core-testing:$archVersion"
// Test helpers for Room
testImplementation "android.arch.persistence.room:testing:1.0.0"
// https://github.com/mockito/mockito
testImplementation 'org.mockito:mockito-core:2.13.0'
androidTestImplementation 'org.mockito:mockito-android:2.13.0'
// AndroidJUnitRunner and JUnit Rules
androidTestImplementation 'com.android.support.test:rules:1.0.1'
androidTestImplementation 'com.android.support.test:runner:1.0.1'
// https://developer.android.com/topic/libraries/testing-support-library/packages.html
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
androidTestCompile 'com.android.support.test.espresso:espresso-idling-resource:3.0.1'
9个回答

142

将testImplementation替换为androidTestImplementation。这样,位于androidTest文件夹中的测试可以访问该库。

dependencies {
    androidTestImplementation "androidx.arch.core:core-testing:2.1.0"
}
如果您未使用 androidx,则请使用 android.arch.core:core-testing:1.1.1

1
我已经进行了上述更改,但这个解决方案在我的情况下没有起作用。还有其他可能性吗? - Kavita Patil
24
我把 androidTestImplementation 改成了 testImplementation,然后问题得到了解决并且可以运行了。 - Boy
2
如果提供的依赖项后仍无法正常工作,请手动添加以下代码: import androidx.arch.core.executor.testing.InstantTaskExecutorRule - Philipp Buhaievskiy

79

我知道现在已经晚了,但我想要在这个被认可的答案中加入一件事情。

如果你想要使用,

@Rule
public InstantTaskExecutorRule instantTaskExecutorRule = 
new InstantTaskExecutorRule();

在你的JUnit测试用例中,即在test文件夹中,请使用以下依赖项,即使用testImplementation

dependencies {
testImplementation "android.arch.core:core-testing:1.0.0"
}

如果你想在你的UI或集成测试用例(androidTest文件夹)中使用InstantTaskExecutorRule,请使用androidTestImplementation。即:

androidTestImplementation "android.arch.core:core-testing:1.0.0"

如果要添加到两个测试类型,请使用androidTestImplementation&testImplementation,即:

androidTestImplementation "android.arch.core:core-testing:1.0.0"

testImplementation "android.arch.core:core-testing:1.0.0"

对于Android X,请使用以下依赖项:

androidTestImplementation 'androidx.arch.core:core-testing:2.0.0'

或者

testImplementation 'androidx.arch.core:core-testing:2.0.0'


9
这应该是最佳答案。 - Idris Stack
感谢@Kavita_p指出... Android大多数情况下都很疯狂,不太友好开发者。如果系统需要在仪器测试和单元测试中具有依赖关系,则应定义testImplementation和androidTestImplementation...想知道,Google何时会将所有这些变得更加开发者友好..!!!! 我发现IOS开发更加开发者友好...这些东西真的很糟糕.... - Sreehari
1
但是如果使用implementation,这个库的代码将会出现在你的发布APK中。这不是必要的,也不正确。如果你想要在两个地方都使用,应该同时使用androidTestImplementation和testImplementation。 - Paulo Pereira
1
嗨,@PauloPereira,感谢您的贡献。是的,您关于在发布APK中包含这些库的观点是正确的。我也会更新我的答案。 - Kavita Patil
是的:!!!! 对于AndroidX,请使用androidTestImplemention(而不仅仅是testImplementation) - Shimon Rothschild

25

针对AndroidX迁移,请添加

androidTestImplementation "androidx.arch.core:core-testing:2.0.0"

1
最新版本信息可以在此处找到 - https://developer.android.com/jetpack/androidx/releases/lifecycle - Hossain Khan

7
dependencies {
    testImplementation "androidx.arch.core:core-testing:2.1.0"
}

6
请将以下两个依赖项添加到您的Gradle文件中:
dependencies {

    // Test helpers for LiveData
    testImplementation "android.arch.core:core-testing:1.0.0"

    // Test helpers for Room
    testImplementation "android.arch.persistence.room:testing:1.0.0"
}

请参阅以下链接获取更多信息: Android 架构组件集成指南

1
有时测试依赖问题可能是选择适当的构建变体的问题,这取决于您的Gradle配置。在我的情况下,测试仅针对调试构建变体进行配置。

1

0

在我的情况下,我会这样使用。

implementation 'androidx.arch.core:core-testing:2.1.0'

更改testImplementationimplementation

我正在使用Android Studio Dolphin | 2021.3.1 Patch 1。


0

将'androidTestImplementation 'androidx.arch.core:core-testing:1.0.0'更改为2.0.0,不知何故神奇地解决了我的问题。

猜测是因为我使用了androidX。


这并没有回答问题。一旦您拥有足够的声望,您将能够评论任何帖子;相反,提供不需要询问者澄清的答案。- 来自审核 - IRTFM

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接