Unresolved reference kotlintest while using gradle kotlin dsl

2.2k views Asked by At

Here is my build.gradle.kts :

import org.jetbrains.kotlin.config.KotlinCompilerVersion

plugins {
    ...
}

android {
    compileSdkVersion(27)

    defaultConfig {
        ...
    }

    buildTypes {
        ...
    }

    sourceSets {
        getByName("main").java.srcDirs("src/main/java", "src/main/kotlin")
        getByName("test").java.srcDirs("src/test/java", "src/test/kotlin")
        getByName("androidTest").java.srcDirs("src/androidTest/java", "src/androidTest/kotlin")
    }

}

tasks.withType<Test> {
    useJUnitPlatform()
}

dependencies {
    implementation(kotlin("stdlib-jdk7", KotlinCompilerVersion.VERSION))
    implementation("com.android.support:appcompat-v7:27.1.1")

    ...

    //Test
    testImplementation("org.junit.jupiter:junit-jupiter-api:5.1.1")
    testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:5.1.1")

    androidTestImplementation("com.android.support.test:runner:1.0.2")
    androidTestImplementation("com.android.support.test.espresso:espresso-core:3.0.2")
    androidTestImplementation("android.arch.core:core-testing:1.1.1")

    testImplementation("io.kotlintest:kotlintest-runner-junit5:3.1.10")
    testImplementation("io.mockk:mockk:1.8.7")
}

When building it with ./gradlew build no error occurs but if I use the specified codefrom the doc :

val test by tasks.getting(Test::class) {
    useJUnitPlatform { }
}

I get the following error : Task with name 'test' not found in project ':app'.

My settings.gradle.kts:

include(":app")

Does anyone know what's going on here? It's weird AS can suggest me autocompletion and all but while compiling, I get this unresolved reference.

1

There are 1 answers

6
Jeel Vankhede On

Looks like your issue is with this block,

tasks.withType<Test> {
    useJUnitPlatform()
}

If you're not using test task then you can simply remove that code and that error will be gone !


Edit:

If you want to use test task. try like this in project level build.gradle,

tasks.register("test", Test::class) {
    useJUnitPlatform()
}