I am getting this error when running JUnit test in Eclipse:
Class not found com.myproject.server.MyTest
java.lang.ClassNotFoundException: com.myproject.serve
NoClassDefFoundError really means it can't initilize the class. It has nothing to do with finding the class. I got this error when calling trim() on a null String.
JUnit won't show NullPointerException. The string isn't null when running normally because I'm fetching the string from a properties file which is not availible for tests.
My advice is to remove pieces from the class until your tests start passing. Then you can determine which line is giving the error.
For project that does not use maven : This worked for me https://ihategeek.wordpress.com/2012/04/18/eclipse-junit-test-class-not-found/
Adding the jre and project src at the bottom in Order and exports in build path
For me I had to put the project x/src/test/java/ at the bottom of the "order and export" in the "java build path"
Pls check if you have added junit4 as dependency.
e.g
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
This appears to occur because only the source code is compiling when you use mvn clean compile
(I'm using maven 3.1.0 so I'm not sure if it always behaved like this).
If you run mvn test
, the test code will compile as well, but then it runs the tests (which may not be immediately desirable if you're trying to run them through Eclipse.) The way around this is to add test-compile
to your Maven command sequence whenever you do a mvn clean
. For example, you would run mvn clean compile test-compile
.
I had faced the same issue. I solved it by removing the external JUnit jar dependency which I added by download from the internet externally. But then I went to project->properties->build path->add library->junit->choosed the version(ex junit4)->apply.
It automatically added the dependency. it solved my issue.