我刚开始读大学的计算机科学课程,我在使用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的最新版本。


当前回答

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

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

您也可以使缓存无效。

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

其他回答

我也有同样的问题。我重建了项目,这对我很有帮助。

进入构建—>重建项目

之后,如果你正在使用Maven工具,我建议使用选项“重新导入所有Maven项目”

如果没有帮助,尝试其他可能的解决方案:

进入文件——>Invalidate cache /Restart——>Invalidate and Restart

or:

在你的Maven项目结构中,src/main/java右键单击java目录并选择选项Mark directory as——> Sources root类似地对test directory做同样的操作:src/test/java右键单击java目录并选择选项Mark directory as——> test 源根

or:

运行——>编辑配置并在JUnit部分删除测试配置。应用更改。然后尝试运行您的测试。应该自动创建新的配置。

or:

Go to File --> Project Structure, select Modules, then select your proper module and go to the Paths tab. Check options: Radio button Use module compile output path should be selected. Output path should be inside your project. Also Test output path should be directory inside your project. For example it can look similarly: Output path: C:\path\to\your\module\yourModule \target\classesTest Output path: C:\path\to\your\module\yourModule \target\test-classesExclude output paths should be deselected.

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

如果您的测试文件夹已经作为一个单独的模块导入(项目视图中的文件夹图标上显示一个小正方形),这也可能发生。 通过在项目视图中选择测试文件夹并按DEL键来删除模块。 然后开始你的测试。 如果弹出对话框显示错误消息,说明没有选择模块,请从下拉菜单中指定根模块。

只需在项目窗口中的文件上单击鼠标右键并选择

运行您的测试。

现在一切开始正常,可能是因为错误的运行配置正在重新构建。

有趣的是,由于不同的原因,我遇到过很多次这个问题。例如,无效缓存和重新启动也有帮助。

最后,我通过纠正我的输出路径在文件->项目结构->项目->项目编译器输出:absolute_path_of_package/out

例如:/Users/random-guy/myWorkspace/src/DummyProject/out