Java 找不到类:IntelliJ 中的空测试套件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39282282/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 21:01:06  来源:igfitidea点击:

Class Not Found: Empty Test Suite in IntelliJ

javaunit-testingintellij-ideajunit

提问by arnbobo

I'm just starting the computer science program at my college, and I'm having some issues with IntelliJ. When I try to run unit tests, I get the message

我刚刚在我的大学开始计算机科学课程,我在使用 IntelliJ 时遇到了一些问题。当我尝试运行单元测试时,我收到消息

Process finished with exit code 1
Class not found: "edu.macalester.comp124.hw0.AreaTest"Empty test suite.

I also see a message entitled "No tests were found" on the left side of my screen. My test code is here:

我还在屏幕左侧看到一条标题为“未找到测试”的消息。我的测试代码在这里:

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);
    }
}

And my project code is here:

我的项目代码在这里:

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;
}

}

How can I get my tests to work? I'm using the most recent version of IntelliJ IDEA CE.

我怎样才能让我的测试工作?我正在使用最新版本的 IntelliJ IDEA CE。

采纳答案by arnbobo

So, my issue here was with folder names. I had called my code folder Classes 2016/2017, which IntelliJ didn't like. Simply remove the slash (or other offending character in path), re-import the project, and you'll be good to go!

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

回答by Carlos Daniel

First thing, we need to understand why this is happening and the error message is clear:

首先,我们需要了解为什么会发生这种情况并且错误消息很清楚:

Process finished with exit code 1 Class not found: "edu.macalester.comp124.hw0.AreaTest"Empty test suite.

进程完成,退出代码 1 找不到类:“edu.macalester.comp124.hw0.AreaTest”空测试套件。

So this mainly occurs when after the unit test class was created we run the methods (tests) individually before running them from the class level. That is all

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

回答by Marius

I had the same problem and rebuilding/invalidating cache etc. didn't work. Seems like that's just a bug in Android Studio...

我遇到了同样的问题,重建/无效缓存等不起作用。似乎这只是 Android Studio 中的一个错误...

A temporary solution is just to run your unit tests from the command line with:

一个临时的解决方案是从命令行运行你的单元测试:

./gradlew test

See: https://developer.android.com/studio/test/command-line.html

请参阅:https: //developer.android.com/studio/test/command-line.html

回答by radekpakula

I tried all solutions but none of them helped. At the end i run test in debug mode and.... it started to work. Maybe some maven's cache was cleared up. It is difficult to say. It works. Try mvn test -X

我尝试了所有解决方案,但没有一个有帮助。最后,我在调试模式下运行测试,然后......它开始工作。也许一些 Maven 的缓存被清除了。很难说。有用。尝试mvn test -X

回答by Schakal

I had the same issue. I rebuilded project, and it helped me.

我遇到过同样的问题。我重建了项目,它帮助了我。

Go to Build--> Rebuild Project

转到构建-->重建项目

After then, if you are using Maven tool, I recommend use option Reimport All Maven Projects

之后,如果您使用 Maven 工具,我建议使用选项Reimport All Maven Projects


If it not help, try another possible solutions:


如果没有帮助,请尝试其他可能的解决方案:

  • Go to File-->Invalidate Caches/Restart--> Invalidate and Restart
  • 转到文件--> 使缓存无效/重新启动-->无效并重新启动

or:

或者:

  • In your Maven project structure src/main/javaright click on javadirectory and select option Mark directory as--> Sources Root

    Similarly do the same with testdirectory so: src/test/javaright click on javadirectory and select option Mark directory as -->Test Sources Root
  • 在您的 Maven 项目结构src/main/java 上右键单击java目录并选择选项Mark directory as--> Sources Root

    同样对test目录执行相同的操作:src/test/java右键单击java目录并选择选项Mark directory作为 -->测试源根

or:

或者:

  • Go to Run--> Edit Configurationsand in section JUnitremove test configurations. Apply changes. After then try to run your tests. New configuration should be created automatically.
  • 转到运行-->编辑配置,然后在JUnit部分删除测试配置。应用更改。然后尝试运行您的测试。应自动创建新配置。

or:

或者:

  • Go to File --> Project Structure, select Modules, then select your proper module and go to the Pathstab.
    Check options:
    Radio button Use module compile output pathshould be selected.

    Output pathshould be inside your project. Also Test output pathshould be directory inside your project. For example it can look similarly:
    Output path:C:\path\to\your\module\yourModule \target\classes
    Test Output path:C:\path\to\your\module\yourModule \target\test-classes

    Exclude output pathsshould be deselected.
  • 转到File --> Project Structure,选择Modules,然后选择合适的模块并转到Paths选项卡。
    检查选项:应选择
    单选按钮使用模块编译输出路径

    输出路径应该在您的项目中。此外,测试输出路径应该是项目内的目录。例如,它看起来类似:
    输出路径:C:\path\to\your\module\yourModule \target\classes
    测试输出路径:C:\path\to\your\module\yourModule \target\test-classes

    排除输出应该取消选择路径

回答by jhegedus

Deleting .ideaand re-importing the SBT project solved this issue for me.

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

回答by Patrick

Does your test require an Android device (emulator or hardware)?
If so, it's called an "instrumented test" and resides in "module-name/src/androidTest/java/".
If not, it's called a "local unit test" and resides in "module-name/src/test/java"

您的测试是否需要 Android 设备(模拟器或硬件)?
如果是这样,它被称为“仪器测试”并驻留在“module-name/src/androidTest/java/”中。
如果不是,则称为“本地单元测试”并驻留在“module-name/src/test/java”中

https://developer.android.com/training/testing/start/index.html

https://developer.android.com/training/testing/start/index.html

I got the same error because I had written a local unit test, but it was placed in the folder for instrumented tests. Moving the local unit test to the "src/test/java" folder fixed it for me.

我遇到了同样的错误,因为我编写了一个本地单元测试,但它被放置在用于检测的文件夹中。将本地单元测试移动到“src/test/java”文件夹为我修复了它。

回答by DStanton

I had a similar problem after starting a new IntelliJ project. I found that the "module compile output path" for my module was not properly specified. When I assigned the path in the module's "compile output path" to the proper location, the problem was solved. The compile output path is assigned in the Project settings. Under Modules, select the module involved and select the Paths tab...

开始一个新的 IntelliJ 项目后,我遇到了类似的问题。我发现我的模块的“模块编译输出路径”没有正确指定。当我将模块的“编译输出路径”中的路径分配到适当的位置时,问题就解决了。编译输出路径在项目设置中分配。在 Modules 下,选择所涉及的模块并选择 Paths 选项卡...

Paths tab in the Project Settings | Modules dialog

项目设置中的路径选项卡 | 模块对话框

screenshot

截屏

...I sent the compiler output to a folder named "output" that is present in the parent Project folder.

...我将编译器输出发送到父项目文件夹中名为“output”的文件夹。

回答by Alexander Klimetschek

Same issue here using IDEA 15.0.6, and nothing helped except when I renamed the package the test class was in. Afterwards I renamed it back to its original name and it still worked, so the rename action might have cleared some cache.

同样的问题在这里使用 IDEA 15.0.6,除了当我重命名测试类所在的包时没有任何帮助。之后我将它重命名回原来的名称并且它仍然有效,所以重命名操作可能已经清除了一些缓存。

回答by Inweo

In my case there was a problem with test name :).

就我而言,测试名称有问题:)。

If name was: dummyNameTestthen get no tests where found, but in case testDummyNameeverything was ok

如果名称是: dummyNameTest然后在找到的地方不进行测试,但万一 testDummyName一切正常

回答by Luis Camargo

Mark your package/directory as Test Sources in your IntelliJ IDEA.

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