Unit tests not working on Android Studio Arctic Fox

12,090

Solution 1

If you're using Android Studio Arctic Fox, you need to modify your app/build.gradle a bit.

Add

android {
    testOptions {
        execution 'ANDROIDX_TEST_ORCHESTRATOR'

        unitTests.all {
            useJUnitPlatform() // <--- this is the important part
        }
    }
}

And also make sure you add junit-engine to your dependencies

testImplementation 'org.junit.jupiter:junit-jupiter-engine:5.7.1'

Solution 2

I have tried some posted solutions but no one worked for me.

The solution That worked for me is to use java 8(version 1.8) not Java that bundled with Android Studio which is (11.0.10) for Arctic fox.

Steps

  • File -> Settings -> Build -> gradle -> gradle JDK
  • choose download JDK
  • choose version 1.8
  • click download
  • after finish click apply and run tests again

[Update] in case of your targeting android 12 So this require AGP 7.0+ which require Java 11 Solution will not be applicable Otherwise above solution will fit.

Solution 3

This might not be the solution for everyone but our project had many unit tests that are broken due to Android Studio Arctic Fox's Gradle test runner. It appears that if you update to the latest Gradle plugin, right clicking on a test class will prompt a new Gradle "Create test" option.Create Gradle test option.

We have not updated the plugin yet so our solution was to make our own Gradle Run Configuration for Unit tests.

This can be done by:

  1. Go to Toolbar > Run > Edit Configurations
  2. Then create a new configuration under Gradle with tasks for the tests you want to run Run Configurations The Old Android JUnit tests are what Android Studio run by default without the plugin update.

Solution 4

It won't be a great solution but for me, it works only when I'm using JDK 1.8 as Gradle JDK.

To change the Gradle JDK please open Settings > Build, Execution, Deployment > Build Tools > Gradle > Gradle JDK.

By clicking the list and using Download JDK... JDK 1.8 can be downloaded.

Solution 5

For me, upgrading Android Studio to Arctic Fox 2020.3.1 Patch 2 (just released)is the right solution.

In the past few days, I was also struggling with fixing all my JUnit test failures with prior versions of Android Studio down to 4.0. None of these old Android Studio versions worked for my JUnit tests. I also tried all the answers posted so far: none of them worked for me. Today, all of my JUnit tests have been successful just by upgrading my Android Studio to Arctic Fox 2020.3.1 Patch 2 without making any changes to my source code. I’m sharing this news to help some of you who are still facing the issue. I've tested the fix on my MacBook.

Updates:

On Linux, after upgrading Robolectric version from 4.3.1 to 4.5.1, all of my JUnit tests are now passing:

dependencies {
    // >= 4.4.1 (but < 4.6) is needed on Linux starting Android Studio Arctic Fox:
    testImplementation 'org.robolectric:robolectric:4.5.1'
}
Share:
12,090
gmartinsnull
Author by

gmartinsnull

Updated on June 06, 2022

Comments

  • gmartinsnull
    gmartinsnull almost 2 years

    I've recently installed Android Studio Arctic Fox v2020.3.1 Beta 2 to be able to use Jetpack Compose in our project since I wasn't able to do so back on AS 4.2.1 because Compose was throwing a strange build error that got fixed on Arctic Fox.

    So here's the problem, after loading the project, updating some SDKs/libraries I was able to build and run no problem, however, when I went to run the unit tests I noticed that all test classes were no longer recognized by the IDE. After some investigation I found out that newer versions of AS will no longer recognize JUnit configurations.

    As you can see in the screenshots below there is no way to run those tests as I normally would:

    enter image description here

    enter image description here

    And this one is from Run/Debug Configurations where the JUnit tests are under the unknown category:

    enter image description here

    Finally, I did create a gradle config to test but it just says on the left side:

    Test events were not received

    And throws an error on the right side:

    Execution failed for task 'features:signup:testDevDebugUnitTest'. No tests found for given includes: com.projectsaturn.android.features:signup.SignupViewModelTest

    I wonder if anyone out there has come across this issue and was able to resolve?

    Any help is appreciated!

    UPDATE 01:

    So I decided to roll everything back and start from scratch. This time I only updated a few things:

    • Gradle: com.android.tools.build:gradle:7.0.0-beta02
    • Kotlin: org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.4.32
    • Crashlytics: com.google.firebase:firebase-crashlytics-gradle:2.6.1
    • Test (core, runner and rules): androidx.test:core/runner/rules:1.3.0

    Now the issue is that all unit tests fail when I run it via Gradle. I suspect that it isn't running the @Before annotated function prior to running the actual tests functions. Or I'm not setting the Gradle test properly (screenshot below)?

    enter image description here

  • Dmytro Karataiev
    Dmytro Karataiev over 2 years
    Could you please elaborate on your answer?
  • Dmytro Karataiev
    Dmytro Karataiev over 2 years
    Not sure thy this answer is downvoted but I believe this is the right approach according to the AS new release article linked in the question.
  • Dr4ke the b4dass
    Dr4ke the b4dass over 2 years
    Replace your import in your test file by import org.junit.jupiter.api.Test instead of import org.junit.Test
  • Lance Kind
    Lance Kind over 2 years
    Doing the above answer requires using the Jupiter annotation for @Test. If you don't do this, you'll get an empty test runner (it won't find your tests), and it'll say "no tasks available."
  • Filipe Oliveira
    Filipe Oliveira over 2 years
    Not all heroes wears capes. It was working on the version of AS that I was using and, suddenly, not working anymore. I updated AS and worked again. Thank you!
  • jonathanzh
    jonathanzh over 2 years
    @ Filipe Oliveira: Glad this answer has been helpful. However, I’ve noticed that my unit tests are still failing on my Ubuntu Linux machine even after upgrading my Android Studio to Arctic Fox 2020.3.1 Patch 2. Maybe a newer version of Android Studio is needed for Linux.
  • Minas Mina
    Minas Mina over 2 years
    Fixed the problem for me, thanks!
  • tonisives
    tonisives over 2 years
    But Android gradle plugin requires Java 11 > Failed to apply plugin 'com.android.internal.application'. > Android Gradle plugin requires Java 11 to run. You are currently using Java 1.8.
  • Mahmoud Mabrok
    Mahmoud Mabrok over 2 years
    You are right in case of your targeting android 12 and this require AGP 7.0+ which require Java 11 Otherwise above solution will fit.
  • Nicolas
    Nicolas over 2 years
    I also needed to add the dependency testImplementation "org.junit.vintage:junit-vintage-engine:5.7.1" for it to work.
  • portfoliobuilder
    portfoliobuilder over 2 years
    I still get empty test runner even after these updated suggestions.
  • marchinram
    marchinram over 2 years
    Doesn't work and no real explanation :(
  • Iker
    Iker about 2 years
    Awesome! I've been trying several "fixes" for several hours and this is the one that really fixed the problem.
  • Rémy Lavergne
    Rémy Lavergne almost 2 years
    Works But I don't know why 🤔