Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot import kotlin.test to use assertFailsWith

For my unit tests, I need to import kotlin.test to be able to use assertFailsWith.

I believe I have the right files in my app build.grade file. Part of the file is:

apply plugin: 'com.android.application'

apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation"org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation 'androidx.appcompat:appcompat:1.0.2'
    implementation 'androidx.core:core-ktx:1.0.2'
    implementation 'com.google.android.material:material:1.0.0'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    implementation 'androidx.vectordrawable:vectordrawable:1.0.1'
    implementation 'androidx.navigation:navigation-fragment:2.0.0'
    implementation 'androidx.navigation:navigation-ui:2.0.0'
    implementation 'androidx.lifecycle:lifecycle-extensions:2.0.0'
    implementation 'androidx.navigation:navigation-fragment-ktx:2.0.0'
    implementation 'androidx.navigation:navigation-ui-ktx:2.0.0'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test.ext:junit:1.1.0'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'

    // ----
    // Per https://developer.android.com/training/testing/unit-testing/local-unit-tests
    //
    // Required -- JUnit 4 framework
    testImplementation 'junit:junit:4.12'
    // Optional -- Robolectric environment
    testImplementation 'androidx.test:core:1.0.0'
    // Optional -- Mockito framework
    testImplementation 'org.mockito:mockito-core:1.10.19'
    // ----


    testImplementation "org.jetbrains.kotlin:kotlin-test-common:$kotlin_version"
    testImplementation "org.jetbrains.kotlin:kotlin-test-annotations-common:$kotlin_version"
}

I am not sure what I am missing...

like image 703
fingia Avatar asked Dec 13 '19 19:12

fingia


1 Answers

With an Android project and JUnit tests, you need to use the kotlin-test-junit module rather than kotlin-test-common or kotlin-test-annotations-common – those are suitable for code shared between platforms in multiplatform projects and are ignored in JVM/Android modules.

Replace the two dependencies with this one:

testImplementation "org.jetbrains.kotlin:kotlin-test-junit:$kotlin_version"
like image 134
hotkey Avatar answered Oct 14 '22 11:10

hotkey