junit 中的 java.lang.NoClassDefFoundError

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

java.lang.NoClassDefFoundError in junit

javaeclipsejunitnoclassdeffounderror

提问by

I am getting this error in java in my junit test code. I looked up on the net and it says that I need to add the junit.jar in the classpath.

我在 junit 测试代码中的 java 中收到此错误。我在网上查了一下,它说我需要在类路径中添加 junit.jar。

In Eclipse I have added it in the classpath of Project Properties windows but I still get initialisation error. What should I do..?

在 Eclipse 中,我已将它添加到项目属性窗口的类路径中,但仍然出现初始化错误。我该怎么办..?

This is the complete trace of the error:

这是错误的完整跟踪:

java.lang.NoClassDefFoundError: org/hamcrest/SelfDescribing
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClassCond(Unknown Source)
    at java.lang.ClassLoader.defineClass(Unknown Source)
    at java.security.SecureClassLoader.defineClass(Unknown Source)
    at java.net.URLClassLoader.defineClass(Unknown Source)
    at java.net.URLClassLoader.access
@ContextConfiguration(locations = { "classpath*:**\*-context.xml", "classpath*:**\*-config.xml" })
0(Unknown Source) at java.net.URLClassLoader.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader.findClass(Unknown Source) at java.lang.ClassLoader.loadClass(Unknown Source) at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source) at java.lang.ClassLoader.loadClass(Unknown Source) at org.junit.internal.builders.JUnit4Builder.runnerForClass(JUnit4Builder.java:13) at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:57) at org.junit.internal.builders.AllDefaultPossibilitiesBuilder.runnerForClass(AllDefaultPossibilitiesBuilder.java:29) at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:57) at org.junit.internal.requests.ClassRequest.getRunner(ClassRequest.java:24) at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.<init>(JUnit4TestReference.java:32) at org.eclipse.jdt.internal.junit4.runner.JUnit4TestClassReference.<init>(JUnit4TestClassReference.java:25) at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.createTest(JUnit4TestLoader.java:41) at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.loadTests(JUnit4TestLoader.java:31) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:452) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197) Caused by: java.lang.ClassNotFoundException: org.hamcrest.SelfDescribing at java.net.URLClassLoader.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader.findClass(Unknown Source) at java.lang.ClassLoader.loadClass(Unknown Source) at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source) at java.lang.ClassLoader.loadClass(Unknown Source) ... 25 more

采纳答案by Chris Dennett

  1. Right click your project in Package Explorer > click Properties
  2. go to Java Build Path > Libraries tab
  3. click on 'Add Library' button
  4. select JUnit
  5. click Next.
  6. select in dropdown button JUnit4 or other new versions.
  7. click finish.
  8. Then Ok.
  1. 在包资源管理器中右键单击您的项目 > 单击属性
  2. 转到 Java 构建路径 > 库选项卡
  3. 单击“添加库”按钮
  4. 选择 JUnit
  5. 点击下一步。
  6. 在下拉按钮中选择 JUnit4 或其他新版本。
  7. 单击完成。
  8. 那么好的。

回答by Thorbj?rn Ravn Andersen

The org/hamcrest/SelfDescribing class is not on the run-time classpath.

org/hamcrest/SelfDescribing 类不在运行时类路径中。

回答by Bin

If you have more than one version of java, it may interfere with your program.

如果您有多个版本的 java,它可能会干扰您的程序。

I suggest you download JCreator.

我建议你下载JCreator。

When you do, click configure, options, and JDK Profiles. Delete the old versions of Java from the list. Then click the play button. Your program should appear.

执行此操作后,单击配置、选项和 JDK 配置文件。从列表中删除旧版本的 Java。然后点击播放按钮。你的程序应该会出现。

If it doesn't, press ctrl+alt+O and then press the play button again.

如果没有,请按 ctrl+alt+O,然后再次按播放按钮。

回答by Amit

Try below steps:

尝试以下步骤:

  1. Go to your project's run configuration. In Classpath tab add JUnit library.
  2. Retry the same steps.
  1. 转到项目的运行配置。在 Classpath 选项卡中添加 JUnit 库。
  2. 重试相同的步骤。

It worked for me.

它对我有用。

回答by Defrag

I had the same issue, the problem was in the @ContextConfiguration in me test classes, i was loading the servlet context too i just change:

我有同样的问题,问题出在我测试类中的 @ContextConfiguration 中,我也在加载 servlet 上下文,我只是改变了:

@ContextConfiguration(locations = { "classpath:**\*-context.xml", "classpath:**\*-config.xml" })

to:

到:

##代码##

and that′s it. this way im only loading all the files with the pattern *-context.xml in me test path.

就是这样。这样我只在我的测试路径中加载所有带有 *-context.xml 模式的文件。

回答by Alex

The same problem can occur if you have downloaded JUnit jar from the JUnit website, but forgotten to download the Hamcrest jar - both are required (the instructions say to download both, but I skipped ahead! Oops)

如果您从 JUnit 网站下载了 JUnit jar,但忘记下载 Hamcrest jar,则可能会出现同样的问题 - 两者都是必需的(说明说要下载两者,但我跳过了!哎呀)

回答by AnandSonake

  1. Make sure Environment variable JUNIT_HOME is set to c:\JUNIT.
  2. then In run configuration > select classpath > add external jars junit-4.11.jar
  1. 确保环境变量 JUNIT_HOME 设置为 c:\JUNIT。
  2. 然后在运行配置>选择类路径>添加外部jar junit-4.11.jar

回答by rishabhdaim

The reason for this is "hamcrest-core" jar is not in classpath as it doesn't comes directly with junit jar. So there are two ways to resolve this:

原因是“hamcrest-core”jar 不在类路径中,因为它不直接随 junit jar 一起提供。所以有两种方法可以解决这个问题:

  1. select project -> buildpath -> add libraries and select junit (It contains both junit & hamcrest-core)
  2. download hamcrest-core from maven repo and add this to your classpath.
  1. 选择项目 -> 构建路径 -> 添加库并选择 junit(它包含 junit 和 hamcrest-core)
  2. 从 maven repo 下载 hamcrest-core 并将其添加到您的类路径中。

回答by Ujjwal

This error also comes if 2 versions of hamcrest-library or hamcrest-core is present in the classpath.

如果类路径中存在 2 个版本的 hamcrest-library 或 hamcrest-core,也会出现此错误。

In the pom file, you can exclude the extra version and it works.

在 pom 文件中,您可以排除额外的版本并且它可以工作。

回答by Tires

When using in Maven, update artifact junit:junit from e.g. 4.8.2 to 4.11.

在 Maven 中使用时,将工件 junit:junit 从例如 4.8.2 更新到 4.11。