Reputation: 4287
It seems like kotlinx-coroutines-test dependency is not working for me as I can't access members of the dependency like TestCoroutineDispatcher
, setMain()
, resetMain()
etc. I was following this doc but can't access the members despite adding the gradle dependency. I tried rebuilding the project and invalidating the cache and restart but nothing seems to work. I have also tried doing androidExtensions {experimental = true}
but still no luck.
build.gradle
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-kapt'
android {
compileSdkVersion 28
defaultConfig {
applicationId "com.example.android.kotlincoroutines"
minSdkVersion 21
targetSdkVersion 28
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
androidExtensions {
experimental = true
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation"org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.1.1'
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.1.1'
implementation 'androidx.appcompat:appcompat:1.1.0-alpha02'
implementation 'com.google.android.material:material:1.1.0-alpha03'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
// ViewModel and LiveData
def lifecycle_version = '2.0.0-beta01'
implementation "androidx.lifecycle:lifecycle-extensions:$lifecycle_version"
implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:2.1.0-alpha02"
testImplementation "androidx.arch.core:core-testing:$lifecycle_version"
// Room for database
def room_version = '2.0.0'
implementation "androidx.room:room-runtime:$room_version"
kapt "androidx.room:room-compiler:$room_version"
def work_version = "1.0.0-rc01"
implementation "android.arch.work:work-runtime:$work_version"
androidTestImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test:runner:1.1.1'
androidTestImplementation 'androidx.test:rules:1.1.1'
androidTestImplementation "com.google.truth:truth:0.42"
androidTestImplementation "androidx.arch.core:core-testing:$lifecycle_version"
testImplementation 'org.jetbrains.kotlinx:kotlinx-coroutines-test:1.3.0-M1'
}
Where am I going wrong here?
Upvotes: 13
Views: 7282
Reputation: 2176
Change "testImplementation" to "implementation"
def coroutines_version = "1.6.4"
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-test:$coroutines_version"
Then in the test class you can use it like
@Test
fun insertUser() = runTest{
}
Upvotes: 0
Reputation: 1
The solution from @Squti worked for me, namely moving the class trying to import the TestCoroutineDispatcher from a package in the src
directory to a package in the test
directory.
Upvotes: 0
Reputation: 2956
Try adding core as a dependency on your test. It solved the problem for me.
testImplementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.1.0")
Upvotes: 0
Reputation: 68
just add :-
testImplementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.1.0")
Source :- Here yigit have given solution link and ssems like working .
Upvotes: 0
Reputation: 4467
Change your library version to 1.3.2
instead of 1.3.0-M1
like so:
testImplementation 'org.jetbrains.kotlinx:kotlinx-coroutines-test:1.3.2'
And be sure that your tests are unit test
in the test
folder, not instrumented test
in androidTest
folder.
Upvotes: 7
Reputation: 183
replacing testImplementation with implementation did the trick for me.
Upvotes: 17