在 Eclipse 中运行 JUnit 单元测试时出现 ClassNotFoundException(使用 Maven)

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

ClassNotFoundException when running JUnit unit tests within Eclipse (using Maven)

eclipseunit-testingmavenmaven-pluginclassnotfoundexception

提问by newguy

I have just upgraded my SpringSource Tools Suite (STS, a variant IDE of Eclipse) to the latest version (v3.6.1). Then all my JUnit unit tests can not be run again. I am getting this error:

我刚刚将我的 SpringSource 工具套件(STS,Eclipse 的变体 IDE)升级到最新版本(v3.6.1)。然后我所有的 JUnit 单元测试都无法再次运行。我收到此错误:

Class not found ClassToTest
java.lang.ClassNotFoundException: ClassToTest
    at java.net.URLClassLoader.run(URLClassLoader.java:200)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:268)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:252)

According to this blog: ClassNotFoundException when running JUnit unit tests within Eclipse (using Maven), it is because of some misconfiguration of Maven plugin in Eclipse. However, in STS 3.6.2 I cannot find this option "Include Modules" in the Maven plugin. How can I fix this problem and re-enable my unit tests?

根据这篇博客: ClassNotFoundException when running JUnit unit tests within Eclipse (using Maven),这是因为 Eclipse 中 Maven 插件的一些错误配置。但是,在 STS 3.6.2 中,我在 Maven 插件中找不到“包含模块”这个选项。如何解决此问题并重新启用我的单元测试?

采纳答案by newguy

OK it seems I'll have to answer this question by myself.

好吧看来我得自己回答这个问题了。

The main reason for this problem is still the m2eclipse eclipse plug-in. The new version of this plugin does not support nested modules in one project. If we really need to see multiple modules we have to remove the old one from the package explorer and create a working set and import the project again using the option "import existing maven projects". The eclipse import wizard will pop up a window and ask you which module to be imported. Then we can select all the modules and finish the import. As a result the working set will contain all the modules of your project and treat each module as a separate project, although in the workspace your modules are still in one project. By doing this it resolves all the problems that I have come across in Eclipse related to classpath, such as not being able to search a class or not being able to run the unit tests and get a java.lang.ClassNotFoundException.

这个问题的主要原因仍然是m2eclipse eclipse插件。此插件的新版本不支持在一个项目中嵌套模块。如果我们真的需要查看多个模块,我们必须从包资源管理器中删除旧的模块并创建一个工作集并使用“导入现有 maven 项目”选项再次导入项目。eclipse导入向导会弹出一个窗口,询问你要导入哪个模块。然后我们可以选择所有模块并完成导入。因此,工作集将包含您项目的所有模块并将每个模块视为一个单独的项目,尽管在工作区中您的模块仍然在一个项目中。通过这样做,它解决了我在 Eclipse 中遇到的与类路径相关的所有问题,

For reference, here is an article to teach you how to create a working set: Working set

作为参考,这里有一篇文章教你如何创建工作集: 工作集

I think you can import the project without creating a new working set but the working set will keep all the modules that belong to your project in one set so it is easier to organize.

我认为您可以在不创建新工作集的情况下导入项目,但工作集会将属于您项目的所有模块保留在一个集中,因此更易于组织。

I wish I had the answer two months ago so I wouldn't have spent much time searching for a solution.

我希望我在两个月前就得到了答案,这样我就不会花太多时间寻找解决方案了。

回答by Tim Long

There are still some back-draws on the current m2e plugin. Unit-test-cases couldn't be run as their including project grouped in a working-set. Following may help:

当前的 m2e 插件仍然存在一些问题。单元测试用例不能作为它们包含在工作集中的项目来运行。以下可能有帮助:

  1. Right click on project including junit-tests.
  2. Select Maven -> Disable Workspace Resolution
  1. 右键单击包含 junit-tests 的项目。
  2. 选择Maven -> 禁用工作区解析

Try then to run your test again.

然后尝试再次运行您的测试。

回答by Kumar Manish

I think I have found the solution, at least this worked for me: right click on the project and choose 'Maven' -> 'Update Dependencies'

我想我已经找到了解决方案,至少这对我有用:右键单击项目并选择“Maven”->“更新依赖项”

Then when I tried , I did not get ClassNotFoundException .

然后当我尝试时,我没有得到 ClassNotFoundException 。

回答by gffny

I tried everything mentioned here and in other posts. Some of the solutions that people proffered were:

我尝试了这里和其他帖子中提到的所有内容。人们提供的一些解决方案是:

  1. Change the output folder for the test classes.
  2. Create a custom builder for the project that would run test-compile from Maven.
  3. Move the Maven dependencies higher in the Order and Export list in the project build path.
  1. 更改测试类的输出文件夹。
  2. 为将从 Maven 运行测试编译的项目创建自定义构建器。
  3. 将 Maven 依赖项在项目构建路径的 Order and Export 列表中移到更高的位置。

There were many, many more but the one that I found to work was as follows:

还有很多很多,但我发现有效的一个如下:

  1. Close the development environment.
  2. Delete the jars used by the project from my local Maven repository.
  3. Open the IDE.
  4. Build the project.
  5. Run the test.
  1. 关闭开发环境。
  2. 从我的本地 Maven 存储库中删除项目使用的 jar。
  3. 打开 IDE。
  4. 构建项目。
  5. 运行测试。

After hours of beating my head against my keyboard and following suggested solutions, this one worked!

经过数小时的敲击键盘并遵循建议的解决方案后,这个方法奏效了!

回答by user5259110

We too faced the same ClassNotFoundException while trying to run JUnit test class. But when we tried using the following steps, it successfully started running.

我们在尝试运行 JUnit 测试类时也遇到了同样的 ClassNotFoundException。但是当我们尝试使用以下步骤时,它成功开始运行。

  1. Select your project.
  2. Click on the project option displayed in toolbar in the eclipse IDE.
  3. Select “clean”.
  4. Now try running the test file.
  1. 选择您的项目。
  2. 单击 Eclipse IDE 工具栏中显示的项目选项。
  3. 选择“清洁”。
  4. 现在尝试运行测试文件。

回答by artbristol

As a work-around, try running mvn test-compile(either in Eclipse or from the command line), then try running the Junit test within Eclipse.

作为变通方法,尝试运行mvn test-compile(在 Eclipse 中或从命令行),然后尝试在 Eclipse 中运行 Junit 测试。