219

When I attempt to run the following test in IntelliJ IDEA I get the message:

"!!! JUnit version 3.8 or later expected:"

It should be noted that this is an Android project I am working on in IntelliJ IDEA 9.

public class GameScoreUtilTest {
    @Test
    public void testCalculateResults() throws Exception {
        final Game game = new Game();

        final Player player1 = new Player();
        {
            final PlayedHole playedHole = new PlayedHole();
            playedHole.setScore(1);
            game.getHoleScoreMap().put(player1, playedHole);
        }
        {
            final PlayedHole playedHole = new PlayedHole();
            playedHole.setScore(3);
            game.getHoleScoreMap().put(player1, playedHole);
        }
        final GameResults gameResults = GameScoreUtil.calculateResults(game);

        assertEquals(4, gameResults.getScore());
    }
}

The full stack trace looks like this...

!!! 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)
    at com.intellij.rt.execution.junit.JUnitStarter.junitVersionChecks(JUnitStarter.java:152)
    at com.intellij.rt.execution.junit.JUnitStarter.canWorkWithJUnitVersion(JUnitStarter.java:136)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:49)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:110)

Process finished with exit code -3
weston
  • 54,145
  • 21
  • 145
  • 203
benstpierre
  • 32,833
  • 51
  • 177
  • 288
  • Intellij 9, the latest patch. – benstpierre Mar 11 '10 at 08:44
  • 1
    I have solution here http://stackoverflow.com/questions/29172698/junit-version-3-8-or-later-expected/32267762#32267762 – voronnenok Aug 28 '15 at 09:42
  • honestly I finally punted on using Android plugin in IntelliJ and bit the bullet and use the latest Android Studio. all is fine – Kirby Mar 05 '18 at 23:11
  • Incase someone else came here without having "Test" in their Test class' name; you should add "Test" brother. Naming your class "GameEngine" would cause the same error, you can solve it by making it "GameEngineTest". Cheers! – recepinanc Nov 22 '19 at 19:08

27 Answers27

365

This problem happens because Android Platform (android.jar) already contains JUnit classes. IDEA test runner loads these classes and sees that they are from the old JUnit, while you are trying to use annotated tests which is a feature of the new JUnit, therefore you get the error from the test runner.

The solution is simple, open the Project Structure | Modules | Dependencies, and move the junit-4.7.jar up, so that it comes before Android 1.6 Platform in the classpath. Now the test runner will be happy as it loads the new JUnit version.

CrazyCoder
  • 389,263
  • 172
  • 990
  • 904
  • 12
    Just to make people aware this is still an issue with later versions of android. – Chris.Jenkins Dec 28 '12 at 17:54
  • solved the problem in maven as well by placing the android provided dependency after the test dependencies – avianey Nov 15 '13 at 12:18
  • 18
    I have a gradle android project that uses robolectric as described here: http://www.peterfriese.de/android-testing-with-robolectric/ and when I move the Android Platform away from 1st in the classpath, I get the following exception: `Class not found: "com.example.intellijgradletest.MainActivityTest"` – Heath Borders Feb 04 '14 at 15:38
  • 7
    In IDEA 14.0.3 for changing the dependenices order, you should use the arrows. Drag and Drop doesn't work. – david.perez Feb 19 '15 at 10:24
  • how to open the project structure? i mean in Android studio – John Jul 17 '15 at 14:43
  • 7
    In android studio, the depenedency files are autogenerated. Hence it is not possible to move up/down. Is there any Other way around to solve this issue? – iMDroid Sep 28 '15 at 12:29
  • The latest Android Studio might have this issue with slightly different case, **the test class/method should be fall under `Android Tests` instead of `JUnit` in `Run/Debug Configurations`**. See my answer below. – CallMeLaNN Feb 10 '16 at 18:15
  • How is this a solution? I don't want to have to rely on local configuration to get the tests to work. – Patrick Apr 05 '17 at 14:31
  • @for3st this actually changes the order in the gradle file as far as I can see. – weston May 30 '17 at 18:32
  • Yeah, all you need to do in current versions of Android Studio is put JUnit higher up in the module's build.gradle dependency list – Chris Watts Nov 30 '17 at 10:38
  • This also solves the same issue when using Flex. Move the JUnit module to the very first slot after Java and . There are days when I miss the old JPad editor. – Steve Gelman Aug 13 '18 at 18:39
  • Giving another bump for this since it also helped me out with IntelliJ 2018.1.7 and JUnit 4.12. – JCrooks May 21 '19 at 18:44
36

enter image description here

my module is a java library module, so changing JRE to 1.8 java solved the issue.

Or, you can also do it globally via Module Settings > SDK Location > JDK, specifying Oracle's JDK 8 instead of Android SDK's copy.

Gabor
  • 7,352
  • 4
  • 35
  • 56
nutella_eater
  • 3,393
  • 3
  • 27
  • 46
  • This actually works and also makes sense given the problem context. – AgentKnopf Jul 27 '17 at 11:43
  • In my case I needed to change JRE for Junit in Defaults – Rafael Apr 25 '18 at 06:16
  • Another way: Edit your **jdk.table.xml** at `~/Library/Preferences/AndroidStudioX.X/options/jdk.table.xml` or `C:\Users\Name\.AndroidStudioX.X\config\options\jdk.table.xml` on Windows. Find the node `` and set its `` to `` . Also ensure the `` node contains `platforms/android-28/...` in both file path URLs. Adjust "Android-SDK" to your folder name. – Mr-IDE Nov 05 '19 at 16:24
  • This works for me! Just change from Default(Android API 28) to 1.8 (Java) and it works! – aiueoH Nov 12 '19 at 07:55
8

I had this problem with a multi module project (libgdx). One module is pure Java and has tests. My solution was to set "use alternative JRE" to "Java 1.8" in the run configuration of my unit tests. This makes sure no android.jar is on the classpath and the junit 4.x runner is used.

Uwe Post
  • 518
  • 4
  • 10
  • You don't really need to use 1.8. It's enough to choose 1.7 that does not come with the Android sdk. – kingston Jan 16 '16 at 19:48
8

I got the same error when creating both Unit Test and Android Instrument Test in Android Studio 1.4+ and it started to get confused. To avoid this error make sure your test class is fall under Android Tests on Run/Debug Configurations

  1. Make sure you follow the instruction properly https://developer.android.com/training/testing/unit-testing/instrumented-unit-tests.html
  2. Make sure Test Artifact in Build Variants is set to Android Instrumentation Tests
  3. Click menu Run > Edit Configuration
  4. Make sure your class/method name is inside Android Tests instead of JUnit
  5. If it is in JUnit simply delete the config and right click on the file you want to test and Run again. It will then create the config under Android Tests section and it run on device/emulator.
CallMeLaNN
  • 8,328
  • 7
  • 59
  • 74
  • 5
    THIS WORKED FOR ME => If it is in JUnit simply delete the config and right click on the file you want to test and Run again. It will then create the config under Android Tests section and it run on device/emulator. – Devendra Vaja Aug 24 '16 at 21:24
  • Worked for me as well. I ran the same test after moving it from `test` folder to `androidTest` folder. Android Studio didn't update run configuration after doing this – Daniil Nov 22 '19 at 21:06
5

For Android Studio - starting from Android Studio 1.1 Beta 4, Google has added support for Android Gradle plugin 1.1.0-RC. The new plugin supports Unit Testing through Android Studio using junit 4+.

This is still experimental and there are some manual steps to set this up.

ZahiC
  • 13,567
  • 3
  • 25
  • 27
  • It works but Android Studio still have issues with auto-generating configurations for tests so each time you have to fix running configuration by re-selecting test package. Otherwise NullPointerException during test run. – Alfishe Feb 22 '15 at 03:15
4

For everyone who is reading this post and still have the same issue with AndroidStudio 1.0. You cannot change the dependency order in AndroidStudio has the IDE re-write them automatically. And, even if you manage to change the order by modifying the .iml file, you will get a "class not found...". This is because the Test output path cannot be set on AndroidStudio.

Actually, there is solution to make AndroidStudio, Junit and Robolectric working together. Take a look at this https://github.com/JCAndKSolutions/android-unit-test and use this plugin as well : https://github.com/evant/android-studio-unit-test-plugin

Works perfectly for me.

rontho
  • 439
  • 4
  • 11
4

For me this problem was caused by an outdated/broken run configuration for the tests. I simply had to delete the configuration, then create a new one and the problem was fixed.

Delete the old test configuration

shocking
  • 848
  • 12
  • 16
2

I have got the same error when i have create my own junit package

enter image description here

To fix this, i have added these two lines in my app gradle file as it's explained here :

dependencies {
    ...
    // Required -- JUnit 4 framework
    testCompile 'junit:junit:4.12'
    // Optional -- Mockito framework
    testCompile 'org.mockito:mockito-core:1.10.19'
}
Kevin ABRIOUX
  • 16,507
  • 12
  • 93
  • 99
2

I got the same message

JUnit version 3.8 or later expected

by a simple beginner's mistake. I had used the same package names and class names on src/main and src/test for a class (the HomeController class in my case):

my-test-project
  +--pom.xml
  +--src
    +--main
      +--com
        +--example
          +--Application.java
          +--controller
            +--HomeController.java
    +--test
      +--com
        +--example
          +--ApplicationTest.java
          +--controller
            +--HomeController.java  <---- same package and class name: not good!

With that, the src/main HomeController class, as well as the src/test HomeController class, had the same full path:

com.example.controller.HomeController.class

The result: any tests that were dependent on the HomeController class have failed.

Either changing the package name and/or the class name has resolved the issue. Here the example, when both, the package name and the class name is changed:

my-test-project
  +--pom.xml
  +--src
    +--main
      +--com
        +--example
          +--Application.java
          +--controller
            +--HomeController.java
    +--test
      +--com
        +--example
          +--test                       <---- added (optional)
            +--ApplicationTest.java
            +--controller
              +--HomeControllerTest.java    <---- changed

Now the fully qualified class names differ. The src/main HomeController class name is:

com.example.controller.HomeController.class

and the src/test HomeHontrollerTest class name is:

com.example.test.controller.HomeControllerTest.class

With the fully qualified class names being unique, the problem disappears.

Olli
  • 1,621
  • 15
  • 18
1

There are two thing I could imagine to happen

  • If your IDE tries to start an Android Junit test that directly runs on the emulator you can't use Junit4.
  • If you accidentally used the junit classes provided from the android jar they can't run on a normal jvm because there are only real compiled classes for the android dalvik vm.
Janusz
  • 187,060
  • 113
  • 301
  • 369
1

This happened to me as well in Android Studio 1.1 - although it should support unit tests without a plugin.

On other machines (same project, same version of AS) I found that when running unit tests, the IDE does not add the android.jar file to the classpath, while in my machine it does.

My best guess was that due to the conversion we did from Maven to Gradle and moving from intellij to AS some cache of settings remained somewhere in my machine that caused android.jar to be added to the classpath.

What I did is to clear all android related caches from my machine (under the c:\users\USRE_NAME folder): .android .AndroidStudio .gradle .m2

After that I reopened the project and the tests worked.

Still trying to understand what went wrong, but this should do the trick for now.

Yossi Shmueli
  • 359
  • 4
  • 8
1

I had this issue in Android Studio 1.5, because I did not know that I had to switch the "Test Artifact" setting in the "Build Variants" (lower left corner of the main window) from "Android Instrumentation Tests" to "Unit Tests". When you do, you can see an ExampleUnitTest.java file in the Project window.

Matthias T
  • 1,230
  • 2
  • 10
  • 24
1

I had the same problem but for another reason. I was on IntelliJ with a regular java gradle project (not android) but the JDK was set to the Android SDK in Project Structure (was the default JDK for some reasons). This is really dumb but IntelliJ wasn't nice enough to indicate me what's wrong, so I got stuck on that.

Winter
  • 3,894
  • 7
  • 24
  • 56
1

This is how I solved it:

Edit Configurations -> Defaults -> Android JUnit -> Add the following to Working Directory:

$MODULE_DIR$

liminal
  • 1,144
  • 2
  • 13
  • 24
1

Worked when I update IDEA version to 2021.2.1.

inferno
  • 684
  • 6
  • 21
0

In Android project I had minifyEnabled = true, after I changed it to false everything worked.

PrzemekTom
  • 1,328
  • 1
  • 13
  • 34
0

If you remove

testOptions {
    unitTests.returnDefaultValues = true
}

from your build.gradle it will work

Chiara
  • 1,867
  • 15
  • 17
0

Go to Project Structure -> Platform Setting, change SDKs to 1.8 solved my problem.

Lin W
  • 289
  • 4
  • 4
0

I followed CrazyCoder's answer but there was no junit file shown in dependencies. so i downloaded one from http://www.java2s.com/Code/Jar/j/Downloadjunitjar.htm, then added it by pressing the plus button on the right. And it worked

0

Turning off "Use embedded JDK" in Project Structure/SDK Location is what helped in my case but I don't know exactly what was the reason it was failing in the first place.

0

Replace your android.jar in libs folder with the latest one. You can download it from here

DB377
  • 403
  • 4
  • 11
0

In AndroidStudio, Open Project Structure -> SDK Location, you can see JDK location, change use "Use embedded JDK" to you own JDK to apply, then change back to "Use embedded JDK", it's maybe work

RKRK
  • 1,284
  • 5
  • 14
  • 18
0

In my case, change JRE in Run Configurations dose solve the problem, but when I click the run button next to the test function, the JRE options will reset to default.

Finally, similar to @CrazyLiu 's answer, in Project Structure - SDK Location - JDK, select Embedded JDK. Because there is no checkbox in Android Studio 3.6.

Chenhe
  • 924
  • 8
  • 19
0

None of the above worked for me (Intellij 2019.3.5 Build #IU-193.7288.26), finally using 're-import all projects' button on the maven pane worked. enter image description here

SystemsInCode
  • 629
  • 7
  • 19
0

For me, i did delete useLibrary 'android.test.runner' line in android {} block at bulid.gradle module file and everything worked fine.

Mohsents
  • 691
  • 11
  • 9
0

I had the same problem in a Java 11 with Spring project, turns out when I tried to run the test, I put the wrong "shorten command" option.

Using the "JAR Manifest" option fixed the issue.

IntelliJ shorten command options

-1

I was also facing the same issue, after changing into build.gradle it's working fine for me.

change your junit version inside build.gradle to:

    testImplementation 'junit:junit:3.8'
Abdul Rizwan
  • 3,904
  • 32
  • 31