Android Studio - with Junit 4.12 “!!! JUnit version 3.8 or later expected:”

31,080

Solution 1

Solved the issue. I was running the JUnit tests as a standard "JUnit test" in Android Studio. I fixed the issue by resetting the configuration as a Android Test.

Solution 2

Just to add one more situation. I was having this error after updating Android Studio to 4.1 on a project using old AGP (3.3.2) and Gradle (4.10.2). What ended up solving the issue for me was removing the android.test.runner library from gradle dependencies, that is removing the following line from the app's module build.gradle:

useLibrary 'android.test.runner'

I probably got lucky to not actually need any of the classes provided by the library for unit or instrumented tests.

Solution 3

If you have updated android studio version to 4.1 wiuth latest gradle plygin. Please comment out

// useLibrary 'android.test.runner' It will fix error for you.

Solution 4

I solve this problem by moving "testCompile 'junit:junit:4.12'" to the first order in build.gradle dependency block

Solution 5

This is apparently an Android Studio 4.1 issue. There's an open ticket for that already: https://issuetracker.google.com/issues/170328018

What really solved the issue for me was reinstalling the Android 11 (API 30) SDK, according to the bug ticket above:

"...there was an issue with API 30 mock jar that was built incorrectly. Can you open Android Studio SDK Manager and try uninstalling the R SDK or any Android SDK Platform 30 please? The Gradle build will auto install it when you try to rebuild after you delete the R SDK."

Share:
31,080
Timothy Frisch
Author by

Timothy Frisch

Engineering software since 2008.

Updated on July 09, 2022

Comments

  • Timothy Frisch
    Timothy Frisch almost 2 years

    Tried out the suggestions on this post, but I still get the error:

    !!! JUnit version 3.8 or later expected:
    
    java.lang.RuntimeException: Stub!
        at junit.runner.BaseTestRunner.<init>(BaseTestRunner.java:5)
        at junit.textui.TestRunner.<init>(TestRunner.java:54)
        at junit.textui.TestRunner.<init>(TestRunner.java:48)
        at junit.textui.TestRunner.<init>(TestRunner.java:41)
    
    Process finished with exit code -3
    

    Not sure what to do from here. Tried reorganizing the order of the Junit dependency. No luck, anyone else get this issue?

    Android Studio V. 1.2.1.1

  • Bulwinkel
    Bulwinkel over 8 years
    This worked for me but it took a bit to work out how to create an Android Test. Go to Edit Configurations, select Android Tests from the menu on the left, click +, and fill in the details on the right.
  • ericharlow
    ericharlow over 8 years
    if you want to run your test as a standard Junit test be sure your build variant in android studio is set to Unit Tests and then create a Junit test configuration.
  • Julian A.
    Julian A. over 5 years
    you, sir, are a scholar and a gentleman!! thank you - your fixed worked.
  • Alejandro Casanova
    Alejandro Casanova over 5 years
    I'm glad it did, best regards!
  • Mr-IDE
    Mr-IDE almost 5 years
    This worked for me, when using Robolectric for unit tests.
  • buggily
    buggily over 3 years
    This fixed the issue for me as well. Thank you!
  • Henrique
    Henrique over 3 years
    That also worked for me after updating to Android Studio 4.1! Thank you so much for posting this! You saved me a few hours of debugging. Cheers
  • Olivier Bonal
    Olivier Bonal over 3 years
    Fixed it for me for Android Studio 4.1 too! Thanks a lot! :-)
  • beli
    beli over 3 years
    Fixed it on Android Studio 4.1
  • Chris
    Chris over 3 years
    This was the only thing that worked for me - thank you!
  • Admin
    Admin over 3 years
    Also had to remove useLibrary 'android.test.base' and useLibrary 'android.test.mock' - thanks!