I\'m trying to run my junit test (to verify that a properties file loads correctly) but
I get ClassNotFoundException
although the class is there and all requir
You may need to do a combination of the above to resolve this problem. I normally get this error when I do
mvn clean install
and see a compile error preventing the my tests from fully compiling.
Assuming the issues resolved by the above have been dealt with, try clicking Project, Clean, and clean the project which contains the test you want to run. Also make sure Build Automatically is checked.
I tried everything mentioned here and in other posts. Some of the solutions that people proffered were:
There were many, many more but the one that I found to work was as follows: Close the development environment. Delete the jars used by the project from my local Maven repository. Open the IDE. Build the project. Run the test.
After hours of beating my head against my keyboard and following suggested solutions, this one worked!
I add this answer as my solution review from the above.
.project
in the main project folder. Use a proper XML Editor otherwise you will get a fatal error
from Eclipse that stats you can not open this project.Java
by adding this <nature>org.eclipse.jdt.core.javanature</nature>
to <natures></natures>
.<buildCommand><name>org.eclipse.jdt.core.javabuilder</name><arguments></arguments></buildCommand>
to <buildSpec></buildSpec>
.Run as JUnit
... SuccessNow: Check if there is no errors suggesting it can be a plugin (any plugin) that has something to do with tests.
In my case:
Spent few hours on trying to guess it!.
I received this error because I had recently created a new workspace and my "Installed JREs" were not set up correctly. Make sure in Preferences -> Java -> Installed JREs
the root folder of your JDK is selected. The default for me in a new workspace was a JRE for some reason.
Another way that this can unfold, and just did for me, is if you have a build error that Eclipse doesn't tell you about. If compiling the unit test fails then there is no .class file and you will get the ClassNotFoundException.
In my case there was a missing 3rd party jar file. When I ran the test I got a pop-up window that said "Errors exist in required project(s)". I work with a huge project and I always get that message because some of the source is not available to eclipse (long story). Also it doesn't say what the errors are. So I click "Continue" and then I get the exception.
Once I realized what was going on it was easy to fix by adding the missing jar to the classpath under Run -> Debug Configurations...
I'm not sure how to best detect or prevent this from happening except to be aware of this possibility when something goes wrong and to review your most recent changes for what might have gone wrong.