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


当前回答

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

其他回答

我的问题是文件夹名称。我把我的代码文件夹命名为Classes 2016/2017, IntelliJ不喜欢这样。只需删除斜杠(或路径中其他令人讨厌的字符),重新导入项目,就可以开始了!

通过在运行单元测试之前手动运行testClasses任务来解决。

我没有编译代码。

为了解决这个问题,我执行了以下操作。

去Maven ->单击执行Maven目标

执行命令

mvn test-compile 

从下拉菜单中选择项目。

现在去运行你的测试,它会起作用的

我解决这个问题的方法是使用文件夹名称和路径。

由于某些原因,我的测试丢失了/java/文件夹,IntelliJ不喜欢这样。

所以从 . . /测试/ com/.。 来 . . /测试/ java / com/.。

这是好的

在Intellij中遵循以下步骤(为了更好地理解,有截图):

转到文件—>项目结构

导航到模块,现在选择Junit测试文件所在的模块,并选择“使用模块编译输出路径”单选按钮。 提到各自的类文件夹路径,类似于所附的屏幕截图。

请申请和好的。 这对我很管用!