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

当前回答

在项目结构/SDK位置中关闭“使用嵌入式JDK”对我的情况有帮助,但我不知道它最初失败的确切原因是什么。

其他回答

我也收到了同样的信息

JUnit version 3.8 or later expected

一个简单的初学者的错误。我在src/main和src/test上使用了相同的包名和类名(在我的例子中是HomeController类):

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!

这样,src/main HomeController类,以及src/test HomeController类,都有相同的完整路径:

com.example.controller.HomeController.class

结果:依赖于HomeController类的所有测试都失败了。

更改包名和/或类名可以解决问题。这里的例子,当包名和类名都被改变时:

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

现在,完全限定类名有所不同。src/main HomeController类名是:

com.example.controller.HomeController.class

src/test HomeHontrollerTest类名为:

com.example.test.controller.HomeControllerTest.class

由于全限定类名是唯一的,问题就消失了。

将libs文件夹中的android.jar替换为最新版本。 你可以从这里下载

对于我来说,我确实在build时删除了android{}块中的useLibrary 'android.test.runner'行。Gradle模块文件和一切工作正常。

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

在AndroidStudio中,Open Project Structure -> SDK Location,你可以看到JDK的位置,将使用“使用嵌入式JDK”改为你自己的JDK来应用,然后更改回“使用嵌入式JDK”,这可能是工作