Java Eclipse 中的 JUnit4

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

JUnit4 in Eclipse

javaeclipsejunit

提问by WhyNotHugo

I'm trying to run some JUnit test units in Eclipse 3.5, but with no luck at all.

我正在尝试在 Eclipse 3.5 中运行一些 JUnit 测试单元,但完全没有运气。

JUnit3 works fine.

JUnit3 工作正常。

When I create the JUnit4 Test unit, Eclipse offers to add the JUnit library to the class-path. I accept, but when I check to see if it was added in the project's properties panel, I can see JUnit4 was added, but no JARS where included.
If I choose Edit, the combo says "JUnit 4", and just below "Current location: Not Found".

当我创建 JUnit4 测试单元时,Eclipse 提供将 JUnit 库添加到类路径中。我接受,但是当我检查它是否已添加到项目的属性面板中时,我可以看到添加了 JUnit4,但没有包含 JARS。
如果我选择Edit,组合会显示“ JUnit 4”,并在“ Current location: Not Found”下方。

When I launch a JUnit4 test, I get an error saying and internal error occurred, NullPointerException.

当我启动 JUnit4 测试时,我收到一条错误消息,说发生了内部错误 NullPointerException

I've read for about two days now, and all references say eclipse INCLUDES JUnit4, but somehow, there seems to be something I'm missing.

我已经读了大约两天了,所有参考资料都说 eclipse INCLUDES JUnit4,但不知何故,似乎我遗漏了一些东西。

I've tried re-creating my projects and creating a test in brand new ones with no luck.

我试过重新创建我的项目并在没有运气的情况下在全新的项目中创建测试。

package test;

import static org.junit.Assert.*;

import org.junit.Test;

public class AuthServiceTest {

    @Test
    public final void testValidateCredentials() {
        fail("Not yet implemented"); // TODO
    }

}

[Edit]

[编辑]

I've added junit-4.8.1.jar to the project's classpath, and eclipse's classpath, but still the same problem.

我已经将 junit-4.8.1.jar 添加到项目的类路径和 eclipse 的类路径中,但仍然是同样的问题。

[Edit2]

[编辑2]

I also added junit-dep-4.8.1.jar, since I'm not sure if these dependencies are necessary, but no change.

我还添加了 junit-dep-4.8.1.jar,因为我不确定这些依赖项是否必要,但没有改变。

采纳答案by WhyNotHugo

It appears that the Eclipse 3.5 from Fedora's repository doesn't include JUnit, and installing the appropriate package didn't include it either.
Another Debian PC presented the same issue. A clean download from eclipse.org solved the problem.

Fedora 存储库中的 Eclipse 3.5 似乎不包含 JUnit,并且安装适当的包也不包含它。
另一台 Debian PC 也出现了同样的问题。从 eclipse.org 干净下载解决了这个问题。

I willreport this bug at some point :)

在某个时候报告这个错误:)

回答by djna

In my Eclipse installation JUnit 4 is provided, it's in plugins\org.junit4_4.3.1\junit.jar

在我的 Eclipse 安装中,提供了 JUnit 4,它位于 plugins\org.junit4_4.3.1\junit.jar

If you can't find it, then I guess that you may need to download it.

如果你找不到它,那么我猜你可能需要下载它。

You can associate your JUnit with the Eclipse JUnit settings in

您可以将您的 JUnit 与 Eclipse JUnit 设置相关联

Windows->Preferences->Java->Build Path->User Libraries

Select JUnit there, and you can add and edit JARs.

在那里选择 JUnit,您可以添加和编辑 JAR。

回答by george mano

Right click on the project name .

右键单击项目名称。

Built Path--> Add Libraries

Select the appropriate library from the list (JUnit 4 in this instance) .

从列表中选择适当的库(在本例中为 JUnit 4)。