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


当前回答

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

进入构建—>重建项目

之后,如果你正在使用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.

其他回答

重新导入项目或模块可以解决这个问题。 在开发时,我通过重命名包名来解决这个问题。但是输出路径和测试输出路径是旧的路径。所以intellij不能从旧路径中找到类。 所以最简单的方法是纠正输出路径和测试输出路径。

在开始一个新的IntelliJ项目后,我也遇到了类似的问题。我发现我的模块的“模块编译输出路径”没有正确指定。当我将模块的“编译输出路径”中的路径分配到适当的位置时,问题就解决了。编译输出路径在Project设置中分配。在“模块”下,选择所涉及的模块并选择“路径”选项卡…

项目设置|模块对话框中的路径选项卡

...我将编译器输出发送到父项目文件夹中名为“output”的文件夹中。

对我来说,这是因为我的项目被编译到项目外部的目录中。在路径中,输出路径是\production\project_name和\test\project_name,这将它们放在C:\production\project_name中。将它们更改为项目的完整路径允许我的测试访问类文件。

我在我的环境中也有同样的问题(MacOS)。我用的是IntelliJ 2016。我有一个Java库项目(gradle)。

我所做的是

从IntelliJ旧版本(如IntelliJ14)打开/导出项目。 这成功地发生了,我通过制作项目和运行测试用例来验证它。 然后通过IntelliJ 2016重新导入该项目。 在那之后,它工作得很好(构建和测试用例运行)。

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

进入构建—>重建项目

之后,如果你正在使用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.