ClassNotFoundException when running JUnit unit tes

2019-03-12 09:32发布

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?

6条回答
放我归山
2楼-- · 2019-03-12 10:01

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:

  1. Right click on project including junit-tests.
  2. Select Maven -> Disable Workspace Resolution

Try then to run your test again.

查看更多
三岁会撩人
3楼-- · 2019-03-12 10:02

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.

查看更多
何必那么认真
4楼-- · 2019-03-12 10:10

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

Then when I tried , I did not get ClassNotFoundException .

查看更多
Animai°情兽
5楼-- · 2019-03-12 10:17

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.

  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.
查看更多
聊天终结者
6楼-- · 2019-03-12 10:20

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.

查看更多
看我几分像从前
7楼-- · 2019-03-12 10:21

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.

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.

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

查看更多
登录 后发表回答