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

当前回答

如果你移除

testOptions {
    unitTests.returnDefaultValues = true
}

从你的身材。Gradle会起作用的

其他回答

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

我在Android Studio 1.1中也遇到过这种情况——尽管它应该支持没有插件的单元测试。

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

我最好的猜测是,由于我们从Maven到Gradle的转换,并从intellij移动到AS的一些设置缓存仍然在我的机器的某个地方,导致android.jar被添加到类路径。

我所做的是清除所有android相关的缓存从我的机器(在c:\users\USRE_NAME文件夹下): .android .AndroidStudio .gradle .m2

在那之后,我重新启动了项目,测试开始工作。

还在试图理解哪里出了问题,但这应该是现在的把戏。

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

对于那些正在阅读这篇文章并且仍然对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

很适合我。

当我更新IDEA版本到2021.2.1时工作。