Unit tests not working on Android Studio Arctic Fox

AndroidAndroid StudioUnit TestingGradleJunit

Android Problem Overview


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

Android Solutions


Solution 1 - Android

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 - Android

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 - Android

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 - Android

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 - Android

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'
}

Solution 6 - Android

Try android studio canary build. I am using bumblebee and all the test features are working the same way as they used to before. https://developer.android.com/studio/preview

Solution 7 - Android

I tried several suggestions including those listed above but the unit tests were still not running.

I stumbled upon this article from the official website and realized that it seems developers (at least those that run tests) also have to download Android studio Bumblebee from the Canary or Dev channel as implied in the article.

There is no option to RUN ANDROID INSTRUMENTED TESTS USING GRADLE in Android studio Arctic fox (available in the stable channel).

So, to run the tests in AS Arctic Fox,

  1. Download, extract and run Android studio Bumblebee from the Canary channel (You can install alongside the stable Arctic Fox version as described here)
  2. In AS Bumblebee, Select File > Settings > Build, Execution, Deployment > Testing (or Android Studio > Preferences > Build, Execution, Deployment > Testing for mac users)

3.Check the box next to Run Android instrumented tests using Gradle and click Apply then Ok

Run the tests and see it run in Android studio BumbleBee.

Go back to android studio Arctic fox, run the tests, and see it run here also!

Solution 8 - Android

To use JUnit 5 in Android Studio Arctic Fox, modify your app/build.gradle file like this:

android {

    // .... other stuff

    testOptions {
        execution 'ANDROIDX_TEST_ORCHESTRATOR'

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

Then add version to junit like this:

testImplementation 'org.junit.jupiter:junit-jupiter:5.8.2'

inside the dependencies block.

Then remove the other versions of junit (for example junit 4) from the dependencies. Then make sure you are referencing the right version in your tests. Here an example for @Test annotations and assertEquals:

//import static org.junit.Assert.assertEquals;
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.Test;
//import org.junit.Test;

class UtilsTest5 {
   @Test
   public void add() {       
      assertEquals(10, 10);
   }
}

Solution 9 - Android

Set up your testing environment as provided on Android docs Build instrumented tests and will work fine.

Don't forget to add AndroidJUnitRunner as a Default.

android {
    defaultConfig {
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }
}

Solution 10 - Android

Nothing works I have tried all of the above answers the only thing that works is to downgrade your android-studio version to 4.2.1 and then the JUnit tests will run

Solution 11 - Android

Same problem here, I've replaced some dependencies as described here https://developer.android.com/training/testing/junit-runner#ato-gradle and it worked for me!

Solution 12 - Android

For me Gradle test shows Test events were not received, when there are actually errors in the code.

The fix is to build the project before testing and fix the errors if there are any.

One option is to call the gradle test from the command line. This will also show the code errors

./gradlew :app:cleanTestDebugUnitTest :app:testDebugUnitTest --tests "VehicleRepositoryTest.testVehicle"

Solution 13 - Android

Following these steps, worked for me. You need to create a separate task for each class or test. I didn't find out any workaround to run the test except this in Android Studio artifox.

  1. Add this to your module i.e app or another module

    testOptions { unitTests.all { useJUnitPlatform() } }

  2. Add :data:cleanTestDebugUnitTest :data:testDebugUnitTest and --tests "com.*" enter image description here

Solution 14 - Android

I installed AS BumbleBee (per recommendation in this thread), added the JUnit library,

testImplementation 'org.junit.jupiter:junit-jupiter:5.8.2'

and removed the junit:4.x

I set up a Gradle Run Configuration but just added the Gradle Task that was listed in the Gradle sidebar menu, testDebugUnitTest, no :app: or :verification:, etc. I selected the new Run Configuration from the dropdown menu and added the settings:

enter image description here

When I select the Run all tests from the Run Configuration dropdown menu, the Gradle task window opens at the bottom of the IDE, all my tests are run and I get a final "18 of 18 tests were run". I still can't run individual tests from the editor like I used to but at least this is better than nothing and my CI server is able to execute the tests from the commandline without failing again using:

./gradlew :app:testDebugUnitTest --tests "com.*"

Solution 15 - Android

After fresh install of Arctic Fox 2020.3.1 Patch 4 on MacOS Monterey 12.1, I also had the

> Test events were not received

After creating a new project with "Basic Activity" I could not run the provided unit tests that were automatically created with the project.

For me the solution was to accept all licences of Android Studio. I have followed this thread: Automatically accept all SDK licences

Go to /Library/Android/sdk/cmdline-tools/latest/bin and run:

./sdkmanager --licenses

In theory, I would expect a popup asking me to accept licences but I have not seen anything when launched Android Studio for the first time.

Solution 16 - Android

You can try:

File -> Settings -> Experimental -> Uncheck "Do not build Gradle task list during Gradle sync"

After that:

File -> Sync Project with Gradle Files

Solution 17 - Android

Comment out
testOptions.unitTests { //includeAndroidResources = true }

Solution 18 - Android

In Arctic Fox, the play/run button should still be visible in the Test classes. What worked for me, was changing the theme of AS (changed it to Atom OneDark, but probably it works with the build in ones)!

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestiongmartinsnullView Question on Stackoverflow
Solution 1 - AndroidTidder JailView Answer on Stackoverflow
Solution 2 - AndroidMahmoud MabrokView Answer on Stackoverflow
Solution 3 - AndroidDhananjay SureshView Answer on Stackoverflow
Solution 4 - AndroidMohsen BeiranvandView Answer on Stackoverflow
Solution 5 - AndroidjonathanzhView Answer on Stackoverflow
Solution 6 - AndroidcodehitheshView Answer on Stackoverflow
Solution 7 - AndroidOluwasegun WahaabView Answer on Stackoverflow
Solution 8 - AndroidNadirspamView Answer on Stackoverflow
Solution 9 - AndroidArditView Answer on Stackoverflow
Solution 10 - AndroidCovid 19View Answer on Stackoverflow
Solution 11 - AndroidLorenzoView Answer on Stackoverflow
Solution 12 - AndroidtonisivesView Answer on Stackoverflow
Solution 13 - AndroidM.MuzammilView Answer on Stackoverflow
Solution 14 - AndroidGailView Answer on Stackoverflow
Solution 15 - AndroidAlisaView Answer on Stackoverflow
Solution 16 - AndroidplplmaxView Answer on Stackoverflow
Solution 17 - Androiduche GodfreyView Answer on Stackoverflow
Solution 18 - AndroidHaris HView Answer on Stackoverflow