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

十年热恋 提交于 2019-12-31 17:54:17

问题


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?

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


回答1:


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.




回答2:


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




回答3:


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 Sources tab in corresponding module.

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.




回答4:


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.




回答5:


Add a solution to JAVA Maven project.

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.



来源:https://stackoverflow.com/questions/19844709/intellij-how-to-jump-to-source-instead-of-compiled-classes-from-failed-unit-te

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!