无法解析符号InstantTaskExecutorRule

tim*_*dev 23 android unit-testing junit4 rx-java2

我打开示例代码BasicRxJavaSample(来自这篇文章Room + RxJava)主要的是:

@Rule
public InstantTaskExecutorRule instantTaskExecutorRule = 
    new InstantTaskExecutorRule();
Run Code Online (Sandbox Code Playgroud)

并且BasicRxJavaSample都可以.但我不能在我的测试中应用这个.这是正在发生的事情:

无法解析符号InstantTaskExecutorRule

在此输入图像描述

手动导入不起作用:

在此输入图像描述

我的自动完成就像这样 在此输入图像描述

但应该如此

在此输入图像描述

我的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'
Run Code Online (Sandbox Code Playgroud)

Pau*_*ira 46

通过androidTestImplementation替换testImplementation.因此对文件夹androidTest的测试可以访问该库.

dependencies {
    androidTestImplementation "android.arch.core:core-testing:1.1.1"
}
Run Code Online (Sandbox Code Playgroud)

  • @Kavita_p 我将`androidTestImplementation`更改为`testImplementation`,然后它解决并工作 (16认同)
  • 如果在提供依赖项后不起作用。手动添加 `import androidx.arch.core.executor.testing.InstantTaskExecutorRule` (2认同)

Kav*_*a_p 22

我知道已经晚了,但是我想为这个已接受的答案添加一件事,如果您想使用,

@Rule
public InstantTaskExecutorRule instantTaskExecutorRule = 
new InstantTaskExecutorRule();
Run Code Online (Sandbox Code Playgroud)

在您的JUnit测试用例中,即在测试文件夹中,然后使用以下依赖项,即与testImplementation

dependencies {
testImplementation "android.arch.core:core-testing:1.0.0"
}
Run Code Online (Sandbox Code Playgroud)

如果要InstantTaskExecutorRule用于UI或集成测试用例,请使用androidTestImplementation。那是:

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

如果要同时添加两者,请使用androidTestImplementationtestImplementation,即:

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

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

  • 这是最好的答案 (5认同)

Abh*_*Das 20

对于androidX迁移,添加

androidTestImplementation "androidx.arch.core:core-testing:2.0.0"
Run Code Online (Sandbox Code Playgroud)


Squ*_*ang 7

dependencies {
    testImplementation "androidx.arch.core:core-testing:2.1.0"
}
Run Code Online (Sandbox Code Playgroud)


Fen*_*tel 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"
}
Run Code Online (Sandbox Code Playgroud)

更多信息请访问此链接, Android 架构组件集成指南