Search code examples
javaandroidunit-testinggradle

Gradle cannot find junit tests in android project in IntelliJ Idea


I have an android java project in Intellij Idea. I am trying to add unit tests to it, in the proper folder of app/src/test/java/<package.name>/ClassNameTest. The test class file and folder was actually automatically created by IntelliJ. Yet, when I try to run tests, no tests are being run at all, though everything compiles. What am I doing wrong?

My project build.gradle:

buildscript {
    repositories {
        google()
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:7.4.1'
    }
}
allprojects {
    repositories {
        google()
        jcenter()
    }
}

My app project build.gradle:

plugins {
    id 'com.android.application'
}

android {
    signingConfigs {
        clickuptimetracker {
            storeFile file('*****')
            storePassword '*****'
            keyAlias 'ClickupTimeTracker'
            keyPassword '*****'
        }
    }

    defaultConfig {
        applicationId "com.kxs.clickuptimetracker"
        minSdkVersion 26
        compileSdk 33
        targetSdkVersion 33
        versionCode 1
        versionName "1.0"
        signingConfig signingConfigs.clickuptimetracker
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"

    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
        debug {
            applicationIdSuffix ".debug"
            debuggable true
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_11
        targetCompatibility JavaVersion.VERSION_11
    }
    packagingOptions {
        jniLibs {
            excludes += ['res/**/*']
        }
        resources {
            excludes += ['res/**/*', 'resources.arsc', 'AndroidManifest.xml']
        }
    }
    namespace 'com.kxs.clickuptimetracker'
}

dependencies {

    implementation 'com.google.android.support:wearable:2.8.1'
    implementation 'com.google.android.gms:play-services-wearable:17.1.0'
    implementation 'androidx.percentlayout:percentlayout:1.0.0'
    implementation 'androidx.legacy:legacy-support-v4:1.0.0'
    implementation 'androidx.recyclerview:recyclerview:1.2.0'
    implementation 'androidx.wear:wear:1.1.0'
    implementation 'com.android.volley:volley:1.2.0'
    implementation 'com.google.code.gson:gson:2.8.6'
    implementation 'androidx.appcompat:appcompat:1.3.0'
    implementation 'com.google.android.material:material:1.3.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
    implementation 'androidx.navigation:navigation-fragment:2.3.5'
    implementation 'androidx.navigation:navigation-ui:2.3.5'
    testImplementation 'junit:junit:4.13.1'
    testImplementation 'org.junit.jupiter:junit-jupiter:5.8.1'
    compileOnly 'com.google.android.wearable:wearable:2.8.1'
    implementation "com.google.dagger:hilt-android:2.44"
}

My Test file is com.kxs.clickuptimetracker.clickup_api.ClickupManagerTest inside app/src/test/java/com/kxs/clickuptimetracker/clickup_api/ClickupManagerTest.java My Tested class is com.kxs.clickuptimetracker.clickup_api.ClickupManager inside app/src/main/java/com/kxs/clickuptimetracker/clickup_api/ClickupManager.java

The contents of the test file is :

package com.kxs.clickuptimetracker.clickup_api;

import static org.junit.jupiter.api.Assertions.*;

class ClickupManagerTest {

    @org.junit.jupiter.api.Test
    void loadTeams() {
        assert(true);
    }

    @org.junit.jupiter.api.Test
    void loadCurrentUser() {
    }

}

When I am running test task from IntelliJ I get:

Executing tasks: [:compileJava, :testClasses, :app:compileReleaseSources, :app:compileReleaseUnitTestSources] in project C:\Users\Sylwek\IntelliJIDEAProjects\ClickupTimeTracker3

> Task :compileJava NO-SOURCE
> Task :processResources NO-SOURCE
> Task :classes UP-TO-DATE
> Task :compileTestJava NO-SOURCE
> Task :processTestResources NO-SOURCE
> Task :testClasses UP-TO-DATE
> Task :app:preBuild UP-TO-DATE
> Task :app:preReleaseBuild UP-TO-DATE
> Task :app:compileReleaseAidl NO-SOURCE
> Task :app:compileReleaseRenderscript NO-SOURCE
> Task :app:generateReleaseBuildConfig UP-TO-DATE
> Task :app:javaPreCompileRelease UP-TO-DATE
> Task :app:generateReleaseResValues UP-TO-DATE
> Task :app:checkReleaseAarMetadata
> Task :app:mapReleaseSourceSetPaths UP-TO-DATE
> Task :app:generateReleaseResources UP-TO-DATE
> Task :app:mergeReleaseResources UP-TO-DATE
> Task :app:createReleaseCompatibleScreenManifests UP-TO-DATE
> Task :app:extractDeepLinksRelease UP-TO-DATE
> Task :app:processReleaseMainManifest
> Task :app:processReleaseManifest
> Task :app:preReleaseUnitTestBuild UP-TO-DATE
> Task :app:javaPreCompileReleaseUnitTest UP-TO-DATE
> Task :app:processReleaseJavaRes NO-SOURCE
> Task :app:processReleaseUnitTestJavaRes NO-SOURCE
> Task :app:processReleaseManifestForPackage
> Task :app:processReleaseResources
> Task :app:compileReleaseJavaWithJavac
Note: Some input files use or override a deprecated API.
Note: Recompile with -Xlint:deprecation for details.

> Task :app:compileReleaseSources
> Task :app:bundleReleaseClassesToCompileJar
> Task :app:compileReleaseUnitTestJavaWithJavac
> Task :app:compileReleaseUnitTestSources

Deprecated Gradle features were used in this build, making it incompatible with Gradle 8.0.

You can use '--warning-mode all' to show the individual deprecation warnings and determine if they come from your own scripts or plugins.

See https://docs.gradle.org/7.5/userguide/command_line_interface.html#sec:command_line_warnings

BUILD SUCCESSFUL in 1m 26s
16 actionable tasks: 8 executed, 8 up-to-date

Build Analyzer results available

When I am trying to run gradle test task, I get:


Deprecated Gradle features were used in this build, making it incompatible with Gradle 8.0.

You can use '--warning-mode all' to show the individual deprecation warnings and determine if they come from your own scripts or plugins.

See https://docs.gradle.org/7.5/userguide/command_line_interface.html#sec:command_line_warnings

BUILD SUCCESSFUL in 1m 10s
36 actionable tasks: 36 up-to-date

When I am trying execute just the test class from IntelliJ context menu, I get:

Execution failed for task ':app:testReleaseUnitTest'.
> No tests found for given includes: [com.kxs.clickuptimetracker.clickup_api.ClickupManagerTest](--tests filter)

* Try:
> Run with --stacktrace option to get the stack trace.
> Run with --info or --debug option to get more log output.
> Run with --scan to get full insights.

Note that there are no instrumentation tests, just junit tests.

When I create a run configuration for JUnit (ommitting gradle) - the tests do actually run, but as I wrote - they never run through gradle tests task nor through context menu of IntelliJ (which tries to run gradle).

UPDATE: After meddling a bit, I discovered, that the issue is with JUnit Jupiter (the newest version of JUnit 5). It seems that it simply doesn't want to discover any tests. When I used JUnit 4 (and replaced accordingly the @Test decorators), tests started working. So I used JUnit4. I am leaving this question here open. Maybe someone will discover the reason? Or maybe Jupiter simply doesn't work on android builds.


Solution

  • I finally found out what I did wrong. Contrary to what JUnit5 documentation says, for android projects in order to make junit5 work, you need to have such a line in module gradle:

    tasks.withType(Test)  {
        useJUnitPlatform()
    }
    

    Android builds don't have the Test task so you cannot put there

    Test {
        useJUnitPlatform()
    }
    

    as the documentation says. This line will only throw errors.