当我试图在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

当前回答

在转换为build之后,我也面临着同样的问题。Gradle对我来说工作得很好。

在构建中更改junit版本。gradle:

    testImplementation 'junit:junit:3.8'

其他回答

我在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.

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

解决方案很简单,打开项目结构|模块|依赖项,并移动junit-4.7.jar,使其在类路径中位于Android 1.6平台之前。现在,测试运行程序将很高兴地加载新的JUnit版本。

我是这样解决的:

编辑配置->默认值-> Android JUnit ->将以下内容添加到工作目录:

$MODULE_DIR$

以上都不适合我(Intellij 2019.3.5 Build #IU-193.7288.26),最后使用maven窗格上的“重新导入所有项目”按钮起作用了。

我在Android Studio 1.5中遇到了这个问题,因为我不知道我必须在“构建变量”(主窗口的左下角)中将“测试工件”设置从“Android仪器测试”切换到“单元测试”。当您这样做时,您可以在Project窗口中看到一个ExampleUnitTest.java文件。