在Eclipse内运行JUnit单元测试时使用ClassNotFoundException(使用Maven) [英] ClassNotFoundException when running JUnit unit tests within Eclipse (using Maven)

查看:1102
本文介绍了在Eclipse内运行JUnit单元测试时使用ClassNotFoundException(使用Maven)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚将我的SpringSource Tools Suite(STS,Eclipse的一个变体IDE)升级到最新版本(v3.6.1)。那么我所有的JUnit单元测试都不能再运行。我收到这个错误:

 找不到ClassToTest 
java.lang.ClassNotFoundException:ClassToTest
at java.net.URLClassLoader $ 1.run(URLClassLoader.java:200)
在java.security.AccessController.doPrivileged(本机方法)
在java.net.URLClassLoader.findClass(URLClassLoader.java:188)
在java.lang.ClassLoader.loadClass(ClassLoader.java:307)
在sun.misc.Launcher $ AppClassLoader.loadClass(Launcher.java:268)
在java.lang.ClassLoader根据这个博客:
ClassNotFoundException在Eclipse中运行JUnit单元测试(使用Maven)
这是因为Eclipse中Maven插件的配置错误。但是,在STS 3.6.2中,我无法在Maven插件中找到此选项Include Modules。如何解决这个问题并重新启用我的单元测试?

解决方案

好的,我需要回答这个问题由我自己。



这个问题的主要原因仍然是m2eclipse eclipse插件。这个插件的新版本不支持一个项目中的嵌套模块。如果我们真的需要看到多个模块,我们必须从包资源管理器中删除旧的模块,并创建一个工作集,并使用选项import existing maven projects再次导入项目。 eclipse导入向导会弹出一个窗口并询问您要导入哪个模块。然后我们可以选择所有模块并完成导入。因此,工作集将包含项目的所有模块,并将每个模块视为单独的项目,尽管在工作区中,模块仍在一个项目中。通过这样做,它解决了我在Eclipse中遇到的与classpath相关的所有问题,例如无法搜索类或无法运行单元测试并获取java.lang.ClassNotFoundException。



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



我想你可以导入项目而不创建新的工作集,但工作集将所有属于您的项目的模块保存在一个集合中,因此更易于组织。



I希望两个月前我有了答案,所以我不会花太多时间寻找一个解决方案。


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:

Class not found ClassToTest
java.lang.ClassNotFoundException: ClassToTest
    at java.net.URLClassLoader$1.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?

解决方案

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.

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.

这篇关于在Eclipse内运行JUnit单元测试时使用ClassNotFoundException(使用Maven)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆