IntelliJ IDEA与Junit 4.7“! JUnit版本3.8或更高版本:“

问题:

当我尝试在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 Platform(android.jar)已经包含JUnit类。 IDEA测试运行器加载这些类,并看到它们来自旧的JUnit,而您正在尝试使用带有注释的测试,这是新JUnit的一项功能,因此您可以从测试运行器中获取错误。
解决方案很简单,打开Project Structure | Modules | Dependencies,并将junit-4.7.jar移动到类路径中之前 Android 1.6 Platform。现在,测试运行器将会加载新的JUnit版本。

 
 
Code问答: http://codewenda.com/topics/python/
Stackoverflow: IntelliJ IDEA with Junit 4.7 “!!! JUnit version 3.8 or later expected:”

*转载请注明本文链接以及stackoverflow的英文链接

发表评论

电子邮件地址不会被公开。 必填项已用*标注

19 − = 16