scala 在 Intellij 上运行测试:找不到类

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22105264/
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-10-22 06:05:33  来源:igfitidea点击:

Running tests on Intellij: Class not found

scalaintellij-ideagradle

提问by gerasalus

I'm evaluating IntelliJ (13.0.2 133.696) and cannot get jUnit tests to run from within the IDE.

我正在评估 IntelliJ (13.0.2 133.696) 并且无法从 IDE 中运行 jUnit 测试。

My project is a multi module gradle project and uses scala.

我的项目是一个多模块 gradle 项目并使用 scala。

Test class is located under src/test/scala/xxx/xxxxx/xxx/xxxx/xxxxx and everytime i try to run from IDE i get the same error:

测试类位于 src/test/scala/xxx/xxxxx/xxx/xxxx/xxxxx 下,每次我尝试从 IDE 运行时,都会遇到相同的错误:

Class not found: "xxx.xxxxx.xxx.xxxx.xxxxx.AccountRepositoryTest"

Test class is nothing fancy, simple jUnit test:

测试类没什么特别的,简单的 jUnit 测试:

@RunWith(classOf[SpringJUnit4ClassRunner])
@ContextConfiguration(classes = Array(classOf[DataConfig], classOf[SettingsConfig]))
class AccountRepositoryTest extends AssertionsForJUnit {

I've found a related question Cannot run Junit tests from IDEA 13.0 IDE for imported gradle projects, but the provided fix (upgrade to 13.0.2) does not work.

我发现了一个相关问题无法从 IDEA 13.0 IDE 为导入的 gradle 项目运行 Junit 测试,但提供的修复(升级到 13.0.2)不起作用。

I've even tried upgrading to the latest EAP, still the same issue.

我什至尝试升级到最新的 EAP,仍然是同样的问题。

回答by Russ Bateman

I looked through some of these answers, fussed with Project Settings, tried a few things, etc. and nothing worked. (Full disclosure: I'm not trying to juggle Gradle here; I'm just using Maven, but I don't see what this has to do with Gradle.)

我浏览了其中的一些答案,对项目设置大惊小怪,尝试了一些东西等等,但没有任何效果。(完全披露:我不是想在这里玩弄 Gradle;我只是在使用 Maven,但我不明白这与 Gradle 有什么关系。)

I'm using IDEA 14.

我正在使用 IDEA 14。

What I found to work, because it just simply seemed IntelliJ had lost its way, was this:

我发现可行的是,因为它只是看起来 IntelliJ 迷失了方向,是这样的:

$ rm -rf .idea project-name.iml

Then relaunched IntelliJ and did File -> Open -> navigate to the root of my project, etc.--in short, just recreated my project.

然后重新启动 IntelliJ 并执行 File -> Open -> 导航到我的项目的根目录等等——简而言之,只是重新创建了我的项目。

IntelliJ got over it. I may have messed something up originally in this project as I had done plenty of refactoring both package- and class names and I had even changed the project name. (It was probably my fault it happened.)

IntelliJ 克服了它。我最初可能在这个项目中搞砸了一些东西,因为我已经对包名和类名进行了大量重构,我什至更改了项目名称。(这可能是我的错。)

回答by bradley.ayers

I had this same problem, and in my case the problem was due to the "Project compiler output" path being left blank in Project Settings.

我遇到了同样的问题,在我的情况下,问题是由于“项目编译器输出”路径在项目设置中留空。

Project Structure configuration screen

Project Structure configuration screen

To fix it I created a classesdirectory in my project root, and set Project compiler outputto the absolute path (use the button to browse).

为了修复它,我classes在我的项目根目录中创建了一个目录,并设置Project compiler output为绝对路径(使用按钮浏览)。

回答by treefolk

Go to Project Settings -> Project.

转到项目设置 -> 项目。

Fill in Project compiler output: ex. D:\repo\Project\out

填写项目编译器输出:例如。D:\repo\Project\out

Go to Module -> Paths

转到模块 -> 路径

Make sure that:

确保:

output path is like D:\repo\Project\out\production

输出路径类似于 D:\repo\Project\out\production

test output path like D:\repo\Project\out\test

测试输出路径,如 D:\repo\Project\out\test

Should work!

应该管用!

回答by jgpATs2w

Simply 'Build > Rebuild Project' worked for me.

简单的“构建>重建项目”对我有用。

回答by Stas

Check Run/Debug configuration for that test "Use classpath and SDK of module:" should point into your module.

检查该测试的运行/调试配置“使用模块的类路径和 SDK:”应该指向您的模块。

In meantime you module must have a Scala facet and that class must be inside the "Test source Folders".

同时,您的模块必须具有 Scala 方面,并且该类必须位于“测试源文件夹”内。

回答by Jet

Make sure your test class package and the class for which you are writing test case are not same. If both test case and the class is having the same package, the compiler will look in the src folder and ignores the test folder.

确保您的测试类包和您为其编写测试用例的类不同。如果测试用例和类具有相同的包,编译器将查看 src 文件夹并忽略测试文件夹。

回答by MaKri

I had the same problem. I changed a path in Module Settings -> Modules -> Paths -> Test output path to my directory for test classes bytecode (exclude output pathson). Now everything works!

我有同样的问题。我在模块设置 -> 模块 -> 路径 -> 测试输出路径中更改了我的测试类字节码目录的路径排除输出路径)。现在一切正常!

回答by RvK

You can try to invalidate the cache and restart. That usually will resolve issues when you add new dependencies / classes.

您可以尝试使缓存无效并重新启动。当您添加新的依赖项/类时,这通常会解决问题。

回答by WebComer

Just make shure the folder of your test file marked as a test folder in Intellij IDEA. That worked for me. If you have multiple directories with source files with the same name, add package to your class source file, if not present!

只需确保您的测试文件的文件夹在 Intellij IDEA 中标记为测试文件夹即可。那对我有用。如果您有多个包含同名源文件的目录,请将包添加到您的类源文件中(如果不存在)!

回答by greene

IDEA restart solved the issue for me.

IDEA 重启为我解决了这个问题。