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


当前回答

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

dependencies {
    testImplementation 'junit:junit:4.12'
}

to

dependencies {
    testCompile 'junit:junit:4.12'
}

其他回答

我在我的环境中也有同样的问题(MacOS)。我用的是IntelliJ 2016。我有一个Java库项目(gradle)。

我所做的是

从IntelliJ旧版本(如IntelliJ14)打开/导出项目。 这成功地发生了,我通过制作项目和运行测试用例来验证它。 然后通过IntelliJ 2016重新导入该项目。 在那之后,它工作得很好(构建和测试用例运行)。

只需在项目窗口中的文件上单击鼠标右键并选择

运行您的测试。

现在一切开始正常,可能是因为错误的运行配置正在重新构建。

在我的例子中,我把其他所有东西都放在了正确的位置,但我正在用kotlin开发一个java库。 我只是忘记应用插件:

apply plugin: 'kotlin-android'

现在它正按照预期工作。

如果项目存在编译问题,则测试可能无法运行。 因此,首先构建项目为build ->构建项目。 成功编译后重新运行测试。

如果没有任何工作,那么只需关闭项目窗口并删除项目,重新导入为Gradle/Maven项目,这将通过覆盖现有的IntelliJ创建的文件来为您设置一切。这将删除创建的无效缓存。

您也可以使缓存无效。

File ->使缓存失效/重新启动

删除.idea并重新导入SBT项目为我解决了这个问题。