I am getting this error when running JUnit test in Eclipse:
Class not found com.myproject.server.MyTest
java.lang.ClassNotFoundException: com.myproject.serve
I fixed my issue by running maven update. Right click project your project > Maven > Update Project
1- mvn eclipse:eclipse
2- project clean all projects
3- restart
I had the same problem with a Gradle project with a test SourceSet with two resource directories.
This snippet comes from a main-module.gradle and adds a resource dir to the test SourceSet:
sourceSets {
test {
resources {
srcDir('../other-module/src/test/resources')
}
}
}
Doing this I had two resource directories related to the test SourceSet of the project main-module:
../other-module/src/test/resources src/test/resources (relative to the main-module folder, automatically added by the java plugin)
I find out that if I had two files with the same name in both the source directories, something in the process resources stage went wrong. As result, no compilation started and for this reason no .class were copied in the bin directory, where JUnit was looking for the classes. The ClassNotFoundException disappeared just renaming one of the two files.
I had a similar problem with my tests and found somewhere in the Web, that you have to go to Build Path in your project's properties and move Maven Dependencies above JRE System Library. That seems to have worked for me.
If this problem occurs in Eclipse only, executing command Project -> Clean... on selected project may help.
In my case, changing the order of Maven Dependencies from Build Path configuration did not work for me. I changed its order from Run configuration.
Follow these steps:
• Go to Run -> Run Configurations...
• Click on the unit test's run configuration and click on the Classpath tab.
• check Use temporary JAR to specify classpath (to avoid classpath length limitations).
May be it is enough and your test works. You should try, so press Run button. If it did not work. Follow all previous steps again and without pressing Run button, go to the next step.
• In sub branches of User Entries move Maven Dependencies above your project or test class.
• Click Run button.