Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ClassNotFoundException when running a junit test in eclipse

I am really desperate why this exception can even occurs ? I am running test in class MyTestIT. And what class is not found ? Class which I run... I tried to clean and build it again in eclipse but with no success

 Class not found it.mytest.MyTestIT
    java.lang.ClassNotFoundException: it.mytest.MyTestIT
        at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
        at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
        at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.loadClass(RemoteTestRunner.java:685)
        at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.loadClasses(RemoteTestRunner.java:421)
        at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:444)
        at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
        at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
        at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)

PS: I forgot to add it was working whole time. But today I just turn on eclipse and I cant run test here. With maven it is still working

like image 747
hudi Avatar asked May 25 '15 17:05

hudi


People also ask

How do I get JUnit to work in Eclipse?

To use JUnit you must create a separate . java file in your project that will test one of your existing classes. In the Package Explorer area on the left side of the Eclipse window, right-click the class you want to test and click New → JUnit Test Case. A dialog box will pop up to help you create your test case.

How do you fix No class Def Found error?

lang. NoClassDefFoundError, which means the Class Loader file responsible for dynamically loading classes can not find the . class file. So to remove this error, you should set your classpath to the location where your Class Loader is present.


1 Answers

Seems like some kind of weird eclipse issue, that will be difficult for us to diagnose, but the brute-force approach will be this: if you have maven on your project, just delete your project from your eclipse workspace (do not delete files), then from command line do:

mvn clean install eclipse:clean eclipse:eclipse

then re-import your existing project back into your eclipse workspace. should work fine after this procedure.

EDIT

Launch your test and switch to Debug perspective. You will see in the Debug view your most recent launch. Select it and go to its properties (shortcut: Alt+Enter). In the "command line" section you should see what was the exact command Eclipse used to launch your unit test. Check the classpath looks ok. It may be just some weird eclipse project setup. Maven will use a different classpath for running your tests, it may be that Eclipse is looking for your unit test in the wrong directory. If you class is in the classspath, then it must work.

like image 143
Peter Perháč Avatar answered Sep 30 '22 07:09

Peter Perháč