Cannot resolve symbol InstantTaskExecutorRule

2019-04-19 02:37发布

问题:

I open example code BasicRxJavaSample (from this article Room+RxJava) The main thing is there:

@Rule
public InstantTaskExecutorRule instantTaskExecutorRule = 
    new InstantTaskExecutorRule();

And BasicRxJavaSample is all ok. But I can not apply this in my test. That's what's going on:

Cannot resolve symbol InstantTaskExecutorRule

And manual import does not work:

My autocompletion works like this

But should be so

My app build.gradle (full gradle here):

// 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'

回答1:

Replace testImplementation by androidTestImplementation. So the tests on folder androidTest can have access to the library.

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


回答2:

for androidX migration, add

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


回答3:

Please put this two dependencies in your gradle file,

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"
}

Further information please go through this link, Android Architecture components integration guide



回答4:

I think that there is a conflict in some of the linked libraries. I got around this, I used blockingGet() and blockingFirst().

and, in the end, I used https://developer.android.com/training/testing/junit-runner.html#using-android-test-orchestrator

androidTestUtil 'com.android.support.test:orchestrator:1.0.1'

this is what you need!