Monday, July 7, 2008

JUnit class not found (not loading) in Eclipse


I get below error when I run jUnit test class n Eclipse:

Class not found com.ucc.csd.server.PhraseGeneratorTest
java.lang.ClassNotFoundException: com.ucc.csd.server.PhraseGeneratorTest
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:316)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:280)
at java.lang.ClassLoader.loadClass(ClassLoader.java:251)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.loadClass(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.loadClasses(RemoteTestRunner.java:425)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:445)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:673)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:386)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)

Solution:
1. Right-click on the test class, go to "run as" option
2. Select "classpath" tab 
3. Select "user entries" option.
4. Click advanced.
5. Select "add folder" and click "Ok"

Sometimes you may have to right-click on project -> properties -> uncheck the default output directory. Then clean your project and run your jUnit tests.