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


当前回答

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

其他回答

在我的案例中,通过进入我的构建,这个问题得到了解决。Gradle和change

dependencies {
    testImplementation 'junit:junit:4.12'
}

to

dependencies {
    testCompile 'junit:junit:4.12'
}

也传达了同样的信息。我必须删除运行/调试配置。

在我的例子中,我之前将单元测试作为本地测试运行。之后,我把我的测试移动到androidTest包,并试图再次运行它。Android Studio记得上次运行配置,所以它试图再次运行它作为一个本地单元测试,这产生了相同的错误。

在删除配置并再次运行测试之后,它生成了一个新的配置并正常工作。

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

我没有编译代码。

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

去Maven ->单击执行Maven目标

执行命令

mvn test-compile 

从下拉菜单中选择项目。

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

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