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


当前回答

我去了

File -> Invalidate Caches/Restart...

然后它对我有用。

其他回答

我也遇到了同样的问题(Android Studio 3.2 Canary 4),我尝试了其他答案中描述的大部分建议——没有任何成功。注意,这发生在我将文件从test文件夹移动到androidTest文件夹之后。它仍然在运行配置中显示为测试,而不是仪器测试。

我最终创建了一个新文件:

用不同的名称创建新的测试类。 从类中复制所有代码。 运行它。 删除旧类。 将新类重命名为所需的名称。

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

首先,我们需要了解为什么会发生这种情况,错误消息是明确的:

进程已完成,退出代码为1 类未找到:"edu.macalester.comp124.hw0.AreaTest"空测试套件。

因此,这主要发生在创建单元测试类之后,我们在从类级别运行方法(测试)之前单独运行它们。就这些

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

得到相同的错误。我的设备没有连接到android studio。当我连接到工作室。它的工作原理。这解决了我的问题。