如何在 Eclipse 中配置 JUnit 的源代码?

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

How do I configure JUnit's Source in Eclipse?

javaeclipsejunit

提问by Hosam Aly

I am using Eclipse Galileo for Java EE, and I want to configure JUnit to show me the source code when I try to navigate to its methods. I've tried attaching source to the JUnit library, but the library definition is not editable. I cannot even find where to configure the JUnit library in the preferences.

我正在使用Eclipse Galileo for Java EE,并且我想配置 JUnit 以在我尝试导航到其方法时向我显示源代码。我尝试将源代码附加到 JUnit 库,但库定义不可编辑。我什至找不到在首选项中配置 JUnit 库的位置。

When I open the Add Library window and choose JUnit, I see a dialog where I can choose the JUnit version, but it shows that Source Location is "not found".

当我打开“添加库”窗口并选择 JUnit 时,我会看到一个对话框,我可以在其中选择 JUnit 版本,但它显示“未找到源位置”。

How can I configure Eclipse to find JUnit's source?

如何配置 Eclipse 以查找 JUnit 的源代码?

采纳答案by Hosam Aly

I downloaded the Eclipse SDK and checked the differences, and I finally got it to work.

我下载了 Eclipse SDK 并检查了差异,我终于让它工作了。

  1. Download this JARinto your eclipse/pluginsdirectory.

  2. Edit the file source.infoin your eclipse/configuration/org.eclipse.equinox.sourcedirectory, and add the following line:

    org.junit4.source,4.5.0.v20090423,plugins/org.junit4.source_4.5.0.v20090423.jar,-1,false

  3. Open the file artifacts.xmlin your eclipsedirectory, and add the following fragment:

    <artifact classifier='osgi.bundle' id='org.junit4.source' version='4.5.0.v20090423'>
      <properties size='2'>
        <property name='artifact.size' value='128389'/>
        <property name='download.size' value='128389'/>
      </properties>
    </artifact>
    
  4. If Eclipse is already open, you'll need to restart it for the changes to be detected.

  1. 将此 JAR下载到您的eclipse/plugins目录中。

  2. 编辑文件source.info在你的eclipse/configuration/org.eclipse.equinox.source目录,并添加以下行:

    org.junit4.source,4.5.0.v20090423,plugins/org.junit4.source_4.5.0.v20090423.jar,-1,false

  3. 打开该文件artifacts.xml在你的eclipse目录,并添加下面的代码片段:

    <artifact classifier='osgi.bundle' id='org.junit4.source' version='4.5.0.v20090423'>
      <properties size='2'>
        <property name='artifact.size' value='128389'/>
        <property name='download.size' value='128389'/>
      </properties>
    </artifact>
    
  4. 如果 Eclipse 已经打开,您需要重新启动它以检测更改。

Note:For Eclipse 3.6 (Helios), you should use the updated JAR(s). See the comments by @Supressingfire and @Milo.

注意:对于 Eclipse 3.6 (Helios),您应该使用更新的 JAR。请参阅@Supressingfire 和@Milo 的评论。

Note: on Eclipse 3.6(Helios), step 3 (artifacts.xml) is not necessary.
Tested on Ubuntu Eclipse 3.6:
Version: Helios Service Release 2
Build id: 20110218-0911

注意:在 Eclipse 3.6(Helios) 上,不需要步骤 3 (artifacts.xml)。
在 Ubuntu Eclipse 3.6 上测试:
版本:Helios Service Release 2
构建 ID:20110218-0911

回答by VonC

As mentioned in this thread, if you have downloaded the SDK version of Galileo, you have the sources of the main plugins.

正如本主题中提到的,如果您下载了 Galileo 的 SDK 版本,那么您就有了主要插件的来源。

For JUnit, this would be:

对于 JUnit,这将是:

 <pathTo>\eclipse-SDK-3.5-win32-x86_64\eclipse\plugins\org.junit4.source_4.5.0.v20090423.jar

You can try to add that as a source file in the Source tab of a JUnit launcher configuration and see if that solves your issue when you are debugging your JUnit test and are stepping through JUnit native methods.

您可以尝试将其添加为 JUnit 启动器配置的“源”选项卡中的源文件,并查看在调试 JUnit 测试和逐步执行 JUnit 本机方法时是否能解决您的问题。

(Note: I have the x64 version of eclipse Galileo, but I believe the file is the same for the x32 or for other platforms)

(注意:我有 eclipse Galileo 的 x64 版本,但我相信 x32 或其他平台的文件是相同的)

回答by Konstantin Tarashchanskiy

@Hosam Aly answer also works in Eclipse 4.3.1:

@Hosam Aly 回答也适用于 Eclipse 4.3.1:

  1. The jar to download is here
  2. The text to append to eclipse\configuration\org.eclipse.equinox.sourceis org.junit.source,4.11.0.v201303080030,plugins/org.junit.source_4.11.0.v201303080030.jar,-1,false
  3. I did not need to change artifacts.xml
  1. 要下载的jar在这里
  2. 要附加的文本eclipse\configuration\org.eclipse.equinox.sourceorg.junit.source,4.11.0.v201303080030,plugins/org.junit.source_4.11.0.v201303080030.jar,-1,false
  3. 我不需要改变 artifacts.xml

回答by u5563257

My eclipse version is 4.6.0, the only solution that has worked so far for me is to download the source file which named org.junit.source_4.12.0.v201504281640.jar from ftp://ftp.osuosl.org/pub/eclipse/eclipse/updates/4.6/R-4.6-201606061100/plugins/, then placed it into eclipse\plugins, afterwards restart eclipse then the source file has bound to the appropiate junit jar file automatically.

我的 eclipse 版本是 4.6.0,到目前为止对我有用的唯一解决方案是从ftp://ftp.osuosl.org/pub/ 下载名为 org.junit.source_4.12.0.v201504281640.jar 的源文件eclipse/eclipse/updates/4.6/R-4.6-201606061100/plugins/,然后放入eclipse\plugins,然后重启eclipse,源文件自动绑定到对应的junit jar文件。

final result

最后结果