Java IntelliJ IDEA with Junit 4.7“!!! JUnit 3.8 或更高版本预期:”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2422378/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 07:24:20  来源:igfitidea点击:

IntelliJ IDEA with Junit 4.7 "!!! JUnit version 3.8 or later expected:"

javaandroidjunitintellij-idea

提问by benstpierre

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

当我尝试在IntelliJ IDEA 中运行以下测试时,我收到消息:

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

“!!! JUnit 3.8 版或更高版本预期:”

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

需要注意的是,这是我在 IntelliJ IDEA 9 中工作的一个Android项目。

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

采纳答案by CrazyCoder

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.

出现此问题是因为 Android Platform ( android.jar) 已经包含 JUnit 类。IDEA 测试运行器加载这些类并看到它们来自旧的 JUnit,而您尝试使用带注释的测试,这是新 JUnit 的一个特性,因此您从测试运行器中得到错误。

The solution is simple, open the Project Structure| Modules| Dependencies, and move the junit-4.7.jarup, so that it comes beforeAndroid 1.6 Platformin the classpath. Now the test runner will be happy as it loads the new JUnit version.

解决方法很简单,打开Project Structure| Modules| Dependencies,然后向上移动junit-4.7.jar,使其位于类路径之前Android 1.6 Platform。现在测试运行器会很高兴加载新的 JUnit 版本。

回答by Janusz

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.
  • 如果您的 IDE 尝试启动直接在模拟器上运行的 Android Junit 测试,则不能使用 Junit4。
  • 如果您不小心使用了 android jar 提供的 junit 类,它们将无法在普通 jvm 上运行,因为 android dalvik vm 只有真正的编译类。

回答by rontho

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.

对于正在阅读这篇文章但仍然在使用 AndroidStudio 1.0 时遇到相同问题的每个人。您不能在 AndroidStudio 中更改依赖顺序,IDE 会自动重新编写它们。而且,即使您设法通过修改 .iml 文件来更改顺序,您也会收到“找不到类...”。这是因为在 AndroidStudio 上无法设置 Test 输出路径。

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

实际上,有一个解决方案可以让 AndroidStudio、Junit 和 Robolectric 协同工作。看看这个https://github.com/JCAndKSolutions/android-unit-test并使用这个插件:https: //github.com/evant/android-studio-unit-test-plugin

Works perfectly for me.

非常适合我。

回答by ZahiC

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+.

对于 Android Studio - 从Android Studio 1.1 Beta 4 开始,Google 增加了对 Android Gradle 插件 1.1.0-RC 的支持。新插件支持使用 junit 4+ 通过 Android Studio 进行单元测试。

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

这仍然是实验性的,有一些手动步骤来设置它。

回答by Yossi Shmueli

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

这在 Android Studio 1.1 中也发生在我身上 - 尽管它应该支持没有插件的单元测试。

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.

在其他机器上(相同项目,相同版本的 AS),我发现在运行单元测试时,IDE 不会将 android.jar 文件添加到类路径中,而在我的机器中它会。

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.

我最好的猜测是,由于我们进行了从 Maven 到 Gradle 的转换以及从 intellij 到 AS 的转换,一些设置缓存保留在我机器中的某个位置,导致 android.jar 被添加到类路径中。

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

我所做的是从我的机器上清除所有与 android 相关的缓存(在 c:\users\USRE_NAME 文件夹下): .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.

仍在试图了解出了什么问题,但这现在应该可以解决问题。

回答by Uwe Post

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.

我在多模块项目(libgdx)中遇到了这个问题。一个模块是纯 Java 并有测试。我的解决方案是在我的单元测试的运行配置中将“使用替代 JRE”设置为“Java 1.8”。这确保了类路径上没有 android.jar 并且使用了 junit 4.x runner。

回答by ScarOnTheSky

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.

我在 Android Studio 1.5 中遇到了这个问题,因为我不知道我必须将“Build Variants”(主窗口的左下角)中的“Test Artifact”设置从“Android Instrumentation Tests”切换到“Unit Tests” ”。执行此操作后,您可以在“项目”窗口中看到一个 ExampleUnitTest.java 文件。

回答by CallMeLaNN

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

在 Android Studio 1.4+ 中创建Unit Test和时,我遇到了同样的错误,Android Instrument Test并且开始感到困惑。为了避免这种错误请确保您的测试类归入Android TestsRun/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 Artifactin Build Variantsis set to Android Instrumentation Tests
  3. Click menu Run> Edit Configuration
  4. Make sure your class/method name is inside Android Testsinstead of JUnit
  5. If it is in JUnitsimply delete the config and right click on the file you want to test and Runagain. It will then create the config under Android Testssection and it run on device/emulator.
  1. 确保您正确遵循说明https://developer.android.com/training/testing/unit-testing/instrumented-unit-tests.html
  2. 确保Test ArtifactinBuild Variants设置为Android Instrumentation Tests
  3. 点击菜单Run>Edit Configuration
  4. 确保您的类/方法名称在里面Android Tests而不是JUnit
  5. 如果是JUnit简单地删除配置并右键单击要测试的文件,然后Run再次单击。然后它将在Android Tests部分下创建配置并在设备/模拟器上运行。

回答by Winter

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.

我有同样的问题,但出于另一个原因。我在 IntelliJ 上有一个常规的 java gradle 项目(不是 android),但 JDK 被设置为 Android SDK Project Structure(由于某些原因是默认的 JDK)。这真的很愚蠢,但 IntelliJ 不够好,无法告诉我出了什么问题,所以我陷入了困境。

回答by Przemo

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

在 Android 项目中minifyEnabled = true,我将其更改为false一切正常后。