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


当前回答

我遇到了同样的问题,最后,我在我的项目中发现了一个重复的测试类,具有相同的包名和类名。

其他回答

这里使用IDEA 15.0.6也有同样的问题,除了重命名测试类所在的包外,没有任何帮助。之后我重命名为它原来的名字,它仍然工作,所以重命名操作可能已经清除了一些缓存。

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

在IntelliJ IDEA中将包/目录标记为测试源。

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

删除和重新创建运行配置也有帮助。