IntelliJ - How to jump to source instead of compil

2019-03-14 11:09发布

问题:

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 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.



回答3:

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.



回答4:

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