如何在 Eclipse 中选择或更改 JUnit 版本?

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

How to select or change JUnit version in Eclipse?

eclipsejuniteclipse-pluginjunit4

提问by trillions

Basically the source code I am using requires a very specific JUnit test version such as:

基本上,我使用的源代码需要一个非常具体的 JUnit 测试版本,例如:

lucene-test-framework-XX.jar
  The compiled Lucene test-framework library.
  Depends on junit 4.10.x (not 4.9.x, not 4.11.x), and Apache Ant 1.7.x (not 1.6.x, not 1.8.x)

However, I am new to Eclipse, where and how do I change the JUnit to 4.10.x?

但是,我是 Eclipse 的新手,我在哪里以及如何将 JUnit 更改为 4.10.x?

Thanks a lot in advance!

非常感谢!

回答by hEADcRASH

I have changed mine out in the past by (specifically) removing the prior, then adding the specific JUnit (External) JAR in my project properties' library settings. The steps are as below:

过去,我通过(特别是)删除了先前的内容,然后在我的项目属性的库设置中添加了特定的 JUnit(外部)JAR 来更改我的内容。步骤如下:

  1. Project > Properties > Java Build Path > Libraries
  2. Click "Add External JARs..." button at right side --> Select your preferred JUnit jar
  3. Click OK button
  1. 项目 > 属性 > Java 构建路径 > 库
  2. 单击右侧的“添加外部 JAR...”按钮 --> 选择您喜欢的 JUnit jar
  3. 单击确定按钮


(source: aggroculture.com)


(来源:aggroculture.com

回答by Heri

Although the question is how to use a very particular version, the title of the question would also be valid for switching just between JUnit3 and JUnit4. This answer says how to do this.

尽管问题是如何使用一个非常特殊的版本,但问题的标题也适用于仅在 JUnit3 和 JUnit4 之间切换。这个答案说明了如何做到这一点。

Since quite a long time the junit library is part of the eclipse distribution. In eclipse Kepler one can switch between JUnit3 and JUnit4, both covered by the effective JUnit Release 4.11.0 (because JUnit3 is still compatible with JUnit4).

很长一段时间以来,junit 库是 eclipse 发行版的一部分。在 eclipse Kepler 中,可以在 JUnit3 和 JUnit4 之间切换,这两者都包含在有效的 JUnit Release 4.11.0 中(因为 JUnit3 仍然与 JUnit4 兼容)。

Right click on your project, select BuildPath/ConfigureBuildPath and select the tab Libraries. Let's suppose there is the library JUnit3 listed and you want to switch to JUnit4. Select the entry JUnit3, then the button "Remove", then "Add library". In the following wizard you choose "JUnit", "next", "JUnit4" in the dropdown and "Finish".

右键单击您的项目,选择 BuildPath/ConfigureBuildPath 并选择选项卡 Libraries。假设列出了 JUnit3 库,并且您想切换到 JUnit4。选择条目 JUnit3,然后选择“删除”按钮,然后选择“添加库”。在以下向导中,您在下拉列表中选择“JUnit”、“next”、“JUnit4”和“Finish”。

Maybe you have noticed in the ProjectExplorer that the JUnit4-Library - in contrast to JUnit3 - includes also the hamcrest-core library (which provides powerful matchers).

也许您已经在 ProjectExplorer 中注意到 JUnit4-Library - 与 JUnit3 相反 - 还包括 hamcrest-core 库(它提供了强大的匹配器)。

回答by Raul Santelices

The problem, at least in Eclipse Luna SR1/SR2, is that Eclipse seems to prioritize the packaged plugin with JUnit 4.11 in "eclipse/plugins/org.junit_4.11.0.v201303080030". Even after trying the answer aboveto force the version I needed, I kept getting the same test errors so it seems that Junit 4.11 was still being used.

问题是,至少在 Eclipse Luna SR1/SR2 中,Eclipse 似乎优先考虑“eclipse/plugins/org.junit_4.11.0.v201303080030”中带有 JUnit 4.11 的打包插件。即使在尝试了上面答案以强制使用我需要的版本之后,我仍然不断收到相同的测试错误,因此似乎仍在使用 Junit 4.11。

The only solution that has worked so far for me is to replace the junit.jar in that directory (eclipse/plugins/org.junit_4.11.0.v201303080030) with the version I wanted.

到目前为止对我有用的唯一解决方案是用我想要的版本替换该目录 (eclipse/plugins/org.junit_4.11.0.v201303080030) 中的 junit.jar。