Java IntelliJ - 如何在“运行”视图中从失败的单元测试跳转到源代码而不是编译类

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

IntelliJ - How to jump to source instead of compiled classes from failed unit tests in the "Run" view

javaunit-testingjunitintellij-idea

提问by krm

When I run my JUnit tests in IntelliJ and one of them fails, I would like to jump to the source code of the failing test by double-clicking it (or by right-clicking it and selecting "Jump to source" or "Show source") in the results view (in the list of failed tests in the "Run" view). However, this feature doesn't always work correctly. Sometimes the source code is shown and sometimes only the compiled ".class" file of the test is shown to me in the editor window. I would always like to see the source code (".java" file) instead of the ".class" file in the editor window after double-clicking the failed test. One solution for it is to click "Attach Sources..." in the editor window, where the ".class" file of the test class is displayed. However, I have to do it for every test file and it takes much time to do it repeatedly for every test. In my opinion IntelliJ should find the source code automatically, because it's present in my Project (it's my own class and its source code is in the project and I run this test from the Project view, where the classes with source code are visible). How can I configure IntelliJ, so that it finds the source code automatically for every unit test which I run?

当我在 IntelliJ 中运行我的 JUnit 测试并且其中一个失败时,我想通过双击它(或通过右键单击它并选择“跳转到源”或“显示源”来跳转到失败测试的源代码") 在结果视图中(在“运行”视图中的失败测试列表中)。但是,此功能并不总是正常工作。有时会显示源代码,有时在编辑器窗口中只显示测试的编译“.class”文件。双击失败的测试后,我总是希望在编辑器窗口中看到源代码(“.java”文件)而不是“.class”文件。一种解决方案是在显示测试类的“.class”文件的编辑器窗口中单击“Attach Sources...”。然而,我必须为每个测试文件都做这件事,而且每次测试都要重复做这件事需要很多时间。在我看来,IntelliJ 应该自动找到源代码,因为它存在于我的项目中(它是我自己的类,它的源代码在项目中,我从项目视图中运行此测试,其中包含源代码的类是可见的)。如何配置 IntelliJ,以便它为我运行的每个单元测试自动找到源代码?

The ".class" files which are shown instead of the ".java" files are contained in a jar file which is built by Maven.

显示的“.class”文件而不是“.java”文件包含在由 Maven 构建的 jar 文件中。

回答by Alper Akture

You should be able to go to the External Libraries in your Project view (File > Project Structure), find the jar that contains the .class file, and right click (or F4). You will see Open Library Settings. From that dialog, you can attach a src folder to the library. enter image description here

您应该能够在项目视图(文件 > 项目结构)中转到外部库,找到包含 .class 文件的 jar,然后右键单击(或 F4)。您将看到打开库设置。在该对话框中,您可以将 src 文件夹附加到库中。在此处输入图片说明

回答by Kuldeep Jain

I had a similar issue. In my case it was due to incorrect Module configuration. Basically you must ensure that the source folder containing the java files is added to Sourcestab in corresponding module.

我有一个类似的问题。就我而言,这是由于模块配置不正确。基本上,您必须确保将包含 java 文件的源文件夹添加到Sources相应模块的选项卡中。

To do this Go to Project Structure (Ctrl + Alt + Shift + S)and select Modules. Choose corresponding module and verify that the source folder containing the java files is correctly added to the Sources Folders. If it is not then add it.

为此,请转到Project Structure (Ctrl + Alt + Shift + S)并选择Modules。选择相应的模块并验证包含 java 文件的源文件夹是否正确添加到Sources Folders. 如果不是,则添加它。

回答by SSB

I Had a similar issue. The issue was still there even after pointing to the correct source folder but I got rid of it by setting the classes folder to the source code's root folder.

我有一个类似的问题。即使指向正确的源文件夹后问题仍然存在,但我通过将类文件夹设置为源代码的根文件夹来摆脱它。

enter image description here

在此处输入图片说明

回答by gohiluk

I got this issue today. The solution for me was to right click on main pom.xml, then maven, then reimport.

我今天遇到了这个问题。我的解决方案是右键单击主 pom.xml,然后是 maven,然后重新导入。

回答by onebraveman

Add a solution to JAVA Maven project.

为 JAVA Maven 项目添加解决方案。

Open 'Libraries' from 'Project settings', search and select the Java jar you want to attach source file. Then you can add source directory by click the '+' button.

从“项目设置”中打开“库”,搜索并选择要附加源文件的 Java jar。然后您可以通过单击“+”按钮添加源目录。

回答by Lamari Alaa

I Had a similar issue. The solution for me was to close project, delete .idea folder than re open the project.

我有一个类似的问题。我的解决方案是关闭项目,删除 .idea 文件夹而不是重新打开项目。