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


当前回答

在我的情况下,有一个问题的测试名称:)。

如果名字是: dummyNameTest则在找到的地方不进行测试,但以防万一 一切正常

其他回答

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

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

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

当你的模块-和/或项目-jdk没有正确配置时,也会发生这种情况。

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

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

dependencies {
    testImplementation 'junit:junit:4.12'
}

to

dependencies {
    testCompile 'junit:junit:4.12'
}

有趣的是,由于不同的原因,我遇到过很多次这个问题。例如,无效缓存和重新启动也有帮助。

最后,我通过纠正我的输出路径在文件->项目结构->项目->项目编译器输出:absolute_path_of_package/out

例如:/Users/random-guy/myWorkspace/src/DummyProject/out