我刚开始读大学的计算机科学课程,我在使用IntelliJ时遇到了一些问题。当我试图运行单元测试时,我得到了消息

Process finished with exit code 1
Class not found: "edu.macalester.comp124.hw0.AreaTest"Empty test suite.

我还在屏幕左侧看到一条标题为“未找到测试”的消息。我的测试代码如下:

package edu.macalester.comp124.hw0;


import org.junit.Test;
import static org.junit.Assert.*;

public class AreaTest {

    @Test
    public void testSquare() {
    assertEquals(Area.getSquareArea(3.0), 9.0, 0.001);
    }

    @Test
    public void testCircle() {
    assertEquals(Area.getCircleArea(3.0), 28.2743, 0.001);
    }
}

我的项目代码在这里:

package edu.macalester.comp124.hw0;

import java.lang.Math;
public class Area {

/**
 * Calculates the area of a square.
 * @param sideLength The length of the side of a square
 * @return The area
 */
public static double getSquareArea(double sideLength) {
    // Has been replaced by correct formula
    return sideLength * sideLength;
}

/**
 * Calculates the area of a circle.
 * @param radius The radius of the circle
 * @return The area
 */
public static double getCircleArea(double radius) {
    // Replaced by correct value
    return radius * 2 * Math.PI;
}

}

我怎样才能让我的测试正常工作?我使用的是IntelliJ IDEA CE的最新版本。


当前回答

当你的模块-和/或项目-jdk没有正确配置时,也会发生这种情况。

其他回答

对我有用的是右键单击项目文件夹-> Maven ->生成源和更新文件夹

如果项目存在编译问题,则测试可能无法运行。 因此,首先构建项目为build ->构建项目。 成功编译后重新运行测试。

如果没有任何工作,那么只需关闭项目窗口并删除项目,重新导入为Gradle/Maven项目,这将通过覆盖现有的IntelliJ创建的文件来为您设置一切。这将删除创建的无效缓存。

您也可以使缓存无效。

File ->使缓存失效/重新启动

对我来说,这个项目是在项目之外编译的。我只是改变了路径。 更改路径(我使用mac)。

进入文件—>项目结构 进入左边的模块。 选择路径,选择单选按钮(使用模块编译输出路径) 提供输出路径和测试输出路径,这是在你的项目 取消选择“排除输出路径”。 进入文件——>单击“无效缓存”并重新启动

我有同样的问题,重建/无效缓存等不工作。似乎这只是Android Studio的一个bug……

一个临时的解决方案是从命令行运行单元测试:

./gradlew test

参见:https://developer.android.com/studio/test/command-line.html

我也遇到了同样的问题,但没有一个解决方案对我有效。

您可以尝试以下步骤:

在Intellij IDEA中关闭项目。 删除文件资源管理器中的.idea文件夹并重新打开项目。 再做一次测试