81

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

enter image description here

And manual import does not work:

enter image description here

My autocompletion works like this enter image description here

But should be so

enter image description here

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'
tim4dev
  • 2,846
  • 2
  • 24
  • 30

9 Answers9

141

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

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

If you are not using androidx then use android.arch.core:core-testing:1.1.1

Sanjeev
  • 4,255
  • 3
  • 28
  • 37
Paulo Pereira
  • 1,750
  • 1
  • 12
  • 23
79

I know it's late but I would like to add one thing to this accepted answer.

If you want to use,

@Rule
public InstantTaskExecutorRule instantTaskExecutorRule = 
new InstantTaskExecutorRule();

in your JUnit test case, i.e., in test folder then use following dependency, i.e, with testImplementation

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

If you want to use InstantTaskExecutorRule for your UI or integration test cases(androidTest folder), use androidTestImplementation. that is:

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

And if you want to add for both, use androidTestImplementation & testImplementation that is:

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

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

For Android-X use below dependency:

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

OR

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

Kavita Patil
  • 1,784
  • 1
  • 17
  • 30
  • 9
    This shoild be the best answer – Idris Stack May 30 '19 at 21:08
  • Thanks for pointing out @Kavita_p .... Android is mostly crazy and not developer friendly. testImplementation and androidTestImplementation should be defined if system needs to have dependency in Instrumentation test and Unit test... Wondering, when Google will make all these as developer friendly..!!!! I found IOS development much more developer friendly... these thinks Really Sucks.... – Sreehari Jul 17 '19 at 07:36
  • 1
    But if you use implementation, this library code will be present on your release APK. Which is not necessary or correct. If you want use on both, you should use both androidTestImplementation and testImplementation. – Paulo Pereira Jul 24 '19 at 10:45
  • 1
    Hi, @PauloPereira thanks for your input. Yes, you are right about including these libs in release APK. I will update my answer as well. – Kavita Patil Jul 26 '19 at 05:00
  • YES:!!!! for AndroidX use androidTestImplemention (not just testImplementation) – Shimon Rothschild Sep 20 '22 at 09:57
25

for androidX migration, add

androidTestImplementation "androidx.arch.core:core-testing:2.0.0"
Abhilash Das
  • 1,388
  • 1
  • 16
  • 22
7
dependencies {
    testImplementation "androidx.arch.core:core-testing:2.1.0"
}
Squall Huang
  • 647
  • 9
  • 20
6

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

Fenil Patel
  • 1,528
  • 11
  • 28
1

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!

tim4dev
  • 2,846
  • 2
  • 24
  • 30
1

Sometimes test dependency issues can be an issue of selecting the appropriate build variant, depending on your Gradle configuration. In my case, tests are configured for the debug build variant only.

caitcoo0odes
  • 484
  • 1
  • 6
  • 17
0

Changing from 'androidTestImplementation 'androidx.arch.core:core-testing:1.0.0' to 2.0.0 somehow magically solved my problem.

Guessing it's because I use androidX.

  • This does not provide an answer to the question. Once you have sufficient [reputation](https://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](https://stackoverflow.com/help/privileges/comment); instead, [provide answers that don't require clarification from the asker](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead). - [From Review](/review/late-answers/33184788) – IRTFM Nov 21 '22 at 23:22
0

In my case, I use like that.

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

Changing testImplementation for implementation.

I am using Android Studio Dolphin | 2021.3.1 Patch 1.

Svape
  • 1
  • 1