Search code examples
androidgradlejunit

Yet another "No tests found" question (testDebugUnitTest). Did I miss a dependency somewhere? Trying to use JUnit5


I've made my first android app (a very simple button tapper) and am currently trying to create test files for it. I've been following the YouTuber Philipp Lackner and have gotten by pretty well despite most of his videos being a year or 2 out of date for the Android Studio (AS) version I'm using.

Being 2 years old now, the Unit Testing video of his chooses to not use JUnit5. I figured I'd give it a go due to how old the video was, but I've run into the following problem:

The error

FAILURE: Build failed with an exception.

What went wrong:

Execution failed for task ':app:testDebugUnitTest'.

No tests found for given includes: [com.example..............sanityCheck](--tests filter)

Simple test file

My only test file so far:

package com.example.....

import com.google.common.truth.Truth.assertThat
import org.junit.jupiter.api.Test

class MainViewModelTest
{
    @Test
    fun sanityCheck()
    {
        val x = 0
        assertThat(x).isEqualTo(0)
    }
}

My config

AS Jellyfish | 2023.3.1 Patch 1

build.gradle.kts (Module :app)

plugins {
    alias(libs.plugins.android.application)
    alias(libs.plugins.jetbrains.kotlin.android)
    alias(libs.plugins.com.google.devtools.ksp)
    alias(libs.plugins.compose.compiler)
}

android {
    namespace = "com.example....."
    compileSdk = 34

    defaultConfig {
        applicationId = "com.example....."
        minSdk = 26
        targetSdk = 34
        versionCode = 1
        versionName = "1.0"

        testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
        vectorDrawables {
            useSupportLibrary = true
        }
    }
    buildTypes {
        release {
            isMinifyEnabled = false
            proguardFiles(getDefaultProguardFile("proguard-android-optimize.txt"),
                          "proguard-rules.pro")
        }
    }
    compileOptions {
        sourceCompatibility = JavaVersion.VERSION_1_8
        targetCompatibility = JavaVersion.VERSION_1_8
    }
    kotlinOptions {
        jvmTarget = "1.8"
    }
    buildFeatures {
        compose = true
    }
    composeOptions {
        kotlinCompilerExtensionVersion = "1.5.1"
    }
    packaging {
        resources {
            excludes += "/META-INF/{AL2.0,LGPL2.1}"
        }
    }
}

dependencies {

    implementation(libs.androidx.core.ktx)
    implementation(libs.androidx.lifecycle.runtime.ktx)
    implementation(libs.androidx.activity.compose)
    implementation(platform(libs.androidx.compose.bom))
    implementation(libs.androidx.ui)
    implementation(libs.androidx.ui.graphics)
    implementation(libs.androidx.ui.tooling.preview)
    implementation(libs.androidx.material3)
    testImplementation(libs.junit.jupiter)
    testCompileOnly(libs.junit.jupiter)
    testRuntimeOnly(libs.junit.platform.launcher)
    testImplementation(libs.google.truth)
    androidTestImplementation(libs.androidx.junit)
    androidTestImplementation(libs.androidx.espresso.core)
    androidTestImplementation(platform(libs.androidx.compose.bom))
    androidTestImplementation(libs.androidx.ui.test.junit4)
    debugImplementation(libs.androidx.ui.tooling)
    debugImplementation(libs.androidx.ui.test.manifest)

    implementation(libs.androidx.lifecycle.viewmodel.compose)

    // Room
    implementation(libs.androidx.room.ktx)
    ksp(libs.androidx.room.compiler)
}

libs.versions.toml

[versions]
activityCompose = "1.9.0"
agp = "8.4.2"
androidx-room = "2.6.1"
com-google-devtools-ksp = "2.0.0-1.0.22"
composeBom = "2024.06.00"
coreKtx = "1.13.1"
espressoCore = "3.5.1"
junitJupiter = "5.8.1"
junitVersion = "1.1.5"
kotlin = "2.0.0"
lifecycleRuntimeKtx = "2.8.2"
lifecycleViewmodelCompose = "2.8.2"
truthVersion = "1.4.2"

[libraries]
androidx-activity-compose = { group = "androidx.activity", name = "activity-compose", version.ref = "activityCompose" }
androidx-compose-bom = { group = "androidx.compose", name = "compose-bom", version.ref = "composeBom" }
androidx-core-ktx = { group = "androidx.core", name = "core-ktx", version.ref = "coreKtx" }
androidx-espresso-core = { group = "androidx.test.espresso", name = "espresso-core", version.ref = "espressoCore" }
androidx-junit = { group = "androidx.test.ext", name = "junit", version.ref = "junitVersion" }
androidx-lifecycle-runtime-ktx = { group = "androidx.lifecycle", name = "lifecycle-runtime-ktx", version.ref = "lifecycleRuntimeKtx" }
androidx-lifecycle-viewmodel-compose = { group = "androidx.lifecycle", name = "lifecycle-viewmodel-compose", version.ref = "lifecycleViewmodelCompose" }
androidx-material3 = { group = "androidx.compose.material3", name = "material3" }
androidx-room-compiler = { group = "androidx.room", name = "room-compiler", version.ref = "androidx-room" }
androidx-room-ktx = { group = "androidx.room", name = "room-ktx", version.ref = "androidx-room" }
androidx-ui = { group = "androidx.compose.ui", name = "ui" }
androidx-ui-graphics = { group = "androidx.compose.ui", name = "ui-graphics" }
androidx-ui-test-junit4 = { group = "androidx.compose.ui", name = "ui-test-junit4" }
androidx-ui-test-manifest = { group = "androidx.compose.ui", name = "ui-test-manifest" }
androidx-ui-tooling = { group = "androidx.compose.ui", name = "ui-tooling" }
androidx-ui-tooling-preview = { group = "androidx.compose.ui", name = "ui-tooling-preview" }
google-truth = { group = "com.google.truth", name = "truth", version.ref = "truthVersion" }
junit-jupiter = { group = "org.junit.jupiter", name = "junit-jupiter", version.ref = "junitJupiter" }
junit-platform-launcher = { group = "org.junit.platform", name = "junit-platform-launcher" }

[plugins]
android-application = { id = "com.android.application", version.ref = "agp" }
com-google-devtools-ksp = {id = "com.google.devtools.ksp", version.ref = "com-google-devtools-ksp"}
compose-compiler = { id = "org.jetbrains.kotlin.plugin.compose", version.ref = "kotlin" }
jetbrains-kotlin-android = { id = "org.jetbrains.kotlin.android", version.ref = "kotlin" }

Things I have already tried

  • Adding the following to the android section of the app gradle file:
testOptions {
  unitTests.all {
    useJUnitPlatform()
  }
}

I suspect this might be the solution to my problem, but useJUnitPlatform is seen as an unresolved reference. I don't know if I've missed something in my gradle file.

  • Adding the following (similar to above) to the project gradle file:
tasks.withType<Test> {
    useJUnitPlatform()
}

Frankly, every time I see this as an answer to someone, the poster merely says to add it to the gradle file (without specifying further) despite there being 2 gradle files.

  • File -> Settings -> Build, Execution, Deployment -> Build Tools -> Gradle -> Run Tests Using

No longer exists in this version of AS.


Solution

  • You have to apply the plugin:

    id("de.mannodermaus.android-junit5") version "1.10.0.0"
    

    Update your plugins block as:

    plugins {
        alias(libs.plugins.android.application)
        alias(libs.plugins.jetbrains.kotlin.android)
        alias(libs.plugins.com.google.devtools.ksp)
        alias(libs.plugins.compose.compiler)
        id("de.mannodermaus.android-junit5") version "1.10.0.0"
    }