当我试图在IntelliJ IDEA中运行以下测试时,我会收到这样的消息:

”! !JUnit 3.8或更高版本:

值得注意的是,这是我在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());
    }
}

完整的堆栈跟踪看起来像这样…

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

当前回答

我在Android Studio 1.4+中创建单元测试和Android仪器测试时遇到了同样的错误,它开始变得混乱。为了避免这个错误,请确保你的测试类在运行/调试配置的Android测试下

Make sure you follow the instruction properly https://developer.android.com/training/testing/unit-testing/instrumented-unit-tests.html Make sure Test Artifact in Build Variants is set to Android Instrumentation Tests Click menu Run > Edit Configuration Make sure your class/method name is inside Android Tests instead of JUnit 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.

其他回答

我有同样的错误,当我创建自己的junit包

为了解决这个问题,我在我的app gradle文件中添加了这两行,如下所示:

dependencies {
    ...
    // Required -- JUnit 4 framework
    testCompile 'junit:junit:4.12'
    // Optional -- Mockito framework
    testCompile 'org.mockito:mockito-core:1.10.19'
}

对于那些正在阅读这篇文章并且仍然对AndroidStudio 1.0有同样问题的人。你不能改变AndroidStudio的依赖顺序,IDE会自动重写它们。而且,即使您设法通过修改.iml文件来改变顺序,也会得到一个“class not found…”。这是因为测试输出路径不能在AndroidStudio上设置。

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

很适合我。

在我的例子中,在Run Configurations中更改JRE可以解决问题,但是当我单击测试功能旁边的Run按钮时,JRE选项将重置为默认值。

最后,类似于@CrazyLiu的回答,在项目结构- SDK位置- JDK中,选择嵌入式JDK。因为在Android Studio 3.6中没有复选框。

我也有同样的问题,但原因不同。我在IntelliJ上使用一个常规的java gradle项目(不是android),但JDK在项目结构中被设置为android SDK(出于某些原因是默认的JDK)。这真的很愚蠢,但IntelliJ没有很好地告诉我哪里出了问题,所以我被困在那里了。

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

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