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

6

17

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?

Sharanshard answered 12/5, 2011 at 6:58 Comment(5)
Is ClassToTest your own class?Conoscenti
Yes, it is my own Testclass which JUnit will run againstSharanshard
Is it working on the command line?Conoscenti
I believe it is. But command line doesn't give as readable information as Eclipse gives me.Sharanshard
Just to make sure, please try mvn test in the command line to make sure it works.Conoscenti
S
5

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.

Sharanshard answered 13/5, 2011 at 5:7 Comment(2)
What is a 'module' in this context?Bony
@Bony he's talking about maven modules.Testicle
K
21

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.

Keddah answered 13/8, 2012 at 16:6 Comment(0)
S
5

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.

Sharanshard answered 13/5, 2011 at 5:7 Comment(2)
What is a 'module' in this context?Bony
@Bony he's talking about maven modules.Testicle
L
4

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 .

Licentiate answered 9/6, 2011 at 11:42 Comment(0)
S
2

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!

Spleenwort answered 17/5, 2013 at 14:49 Comment(1)
can you share how you deleted the jars used by the project from your local maven repo?Suttles
J
2

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.
Jotter answered 24/8, 2015 at 6:50 Comment(0)
M
0

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.

Mcripley answered 12/5, 2011 at 7:48 Comment(1)
No luck. I have actually tried more than 20 work-arounds but the exception is still thereSharanshard

© 2022 - 2024 — McMap. All rights reserved.