62

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

gmartinsnull
  • 1,075
  • 1
  • 12
  • 11
  • 2
    I did the instruction from Android Studio docs you linked on top (for "Android Studio now uses Gradle test runner") and it looks like it worked fine for me. Using AS Arctic Fox beta05 and jUnit 5.7.2. – adek111 Jul 06 '21 at 11:21
  • @adek111 oh good for you! Thank you for the comment. I decided to just take a different approach to the implementation I had. Basically putting this issue aside for later. I'll get back here once Arctic Fox gets released. – gmartinsnull Jul 07 '21 at 21:24
  • 4
    arctic fox is released, i updated yesterday and facing the same no events received issue. did anything worked for you? – ansh sachdeva Jul 30 '21 at 07:00
  • 2
    With Arctic Fox release, getting different results, sometimes test case passes and and some time I get "No events received". message. And also Test with coverage does not work – Karan Sharma Aug 02 '21 at 23:59
  • 1
    Same issue here, tests are running with gradle runner but `@BeforeEach` is completely ignored. – steliosf Aug 05 '21 at 21:07
  • I was facing the same issue (`Test events were not received`) then I found out that there is an import error in my project (quite hidden in test run log). I'm not sure if it was so unclear in previous versions of AS. – Vít Kapitola Aug 12 '21 at 07:55
  • Did you find any solution? Still unable to run unit test in Android arctic fox. – Biswajit Karmakar Aug 12 '21 at 15:41
  • 1
    Getting the same issue. Answers below does not resolve the `Test events were not received` on my end. – Zerosero Aug 20 '21 at 02:05

18 Answers18

27

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'
Tidder Jail
  • 472
  • 7
  • 11
5

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.
Dhananjay Suresh
  • 1,030
  • 1
  • 14
  • 28
  • 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. – Dmytro Karataiev Aug 18 '21 at 16:01
4

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.

Mahmoud Mabrok
  • 1,362
  • 16
  • 24
  • 1
    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. – tonisives Oct 12 '21 at 01:13
  • 2
    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. – Mahmoud Mabrok Oct 13 '21 at 14:03
2

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!

Oluwasegun Wahaab
  • 2,663
  • 3
  • 16
  • 19
2

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.

2

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'
}
jonathanzh
  • 1,346
  • 15
  • 21
  • 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! – Filipe Oliveira Sep 25 '21 at 13:03
  • @ 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. – jonathanzh Sep 26 '21 at 17:00
2

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

2

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"
    }
}
Ardit
  • 168
  • 1
  • 5
1

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!

Lorenzo
  • 116
  • 1
  • 5
1

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);
   }
}
Gourav
  • 2,746
  • 5
  • 28
  • 45
Nadirspam
  • 161
  • 7
  • When I try this, I get an Unresolved reference: junit and I'm told to Add 'JUnit4' to classpath. – Bink Jul 13 '22 at 17:58
0

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

0

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"
tonisives
  • 1,962
  • 1
  • 18
  • 17
0

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

M.Muzammil
  • 643
  • 9
  • 18
0

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.*"
Gail
  • 316
  • 2
  • 13
0

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.

Alisa
  • 1
  • 1
  • 3
0

You can try:

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

After that:

File -> Sync Project with Gradle Files
plplmax
  • 1,765
  • 8
  • 19
0

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

uche Godfrey
  • 111
  • 1
  • 6
-2

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)!

Haris H
  • 125
  • 2
  • 15