24

When I try to run some unit tests, the following error is raised:

java.lang.NoClassDefFoundError: org/junit/runner/manipulation/Filter
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:190)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.loadTestLoaderClass(RemoteTestRunner.java:320)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.createRawTestLoader(RemoteTestRunner.java:310)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.createLoader(RemoteTestRunner.java:305)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.defaultInit(RemoteTestRunner.java:283)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.init(RemoteTestRunner.java:207)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:191)

I have to mention that junit-4.11.jar is added to project build path. Any ideas?

EDIT: I (Gábor Lipták) have read the other question this question supposed to be a duplicate of. This is NOT a duplicate. If someone has Gradle Buildship as build plugin in Eclipse, exactly this error is thrown, if you mistakenly put your test class in main/resorces instead of test/resources. Buildship seems to take care of test vs. compile classpath, when it comes to generate a run configuration. See the following snippets of my .classpath file below as evidence:

<classpathentry kind="src" output="bin/main" path="src/main/resources">
    <attributes>
        <attribute name="gradle_scope" value="main"/>
        <attribute name="gradle_used_by_scope" value="main,test"/>
    </attributes>
</classpathentry>

<classpathentry kind="src" output="bin/test" path="src/test/resources">
    <attributes>
        <attribute name="gradle_scope" value="test"/>
        <attribute name="gradle_used_by_scope" value="test"/>
    </attributes>
</classpathentry>
Gábor Lipták
  • 9,646
  • 2
  • 59
  • 113
lucian.marcuta
  • 1,250
  • 1
  • 16
  • 29
  • Not sure that this is an exact duplicate, the error references a different class and the solution has not been provided on the other page. – bwobbones Nov 30 '16 at 03:54
  • 1
    this should not be marked as duplicate as in the other question the error message clearly shows that hamcrest is missing while in this question the error message tells us that the Filter class cannot be found – coconut Apr 10 '18 at 16:18
  • 2
    I guess your project is not maven based. Run -> Run Configurations Open the tab "Classpath", click the button "Advanced..." on the left Select "Add Library" --> Junit Then, under the 'User Entries', you will see the Junit library. – meadlai May 07 '19 at 09:49

5 Answers5

45

Even I was facing the same issue, so try the below steps -

  1. Right click the project in Package Explorer, and click Properties.
  2. Click the Libraries tab.
  3. Click the Add library button.
  4. Select JUnit and click Next.
  5. Select JUnit 4 (that's what I am using).
  6. Click finish.
  7. Now right click the file containing unit tests and select Properties.
  8. Under the Run/Debug settings, remove any entries from the Launch Configurations for that file. Hit ok.

Hopefully you'll be able to run the tests now.

mmwaikar
  • 655
  • 4
  • 14
  • Voted back up to zero because it was exactly the solution to my problem. Why it would be downvoted, I have no idea. – barnhillec Mar 05 '16 at 21:55
  • 6
    For me it was mostly the second part with deleting the Launch Configurations. Junit might be set up differently in Eclipse (separate lib project, maven dependency, etc.) – Jur_ Jul 13 '16 at 09:07
  • 1
    This did not work for me - my project gets JUnit via gradle and the unit-4.12.jar is in the build path including the Filter class. I think this might be some weird eclipse problem. – coconut Apr 10 '18 at 16:29
  • 1
    For me it was user error: I'd placed the test class in src/main/java instead of src/test/java. – Andy Brown Jul 05 '18 at 13:54
  • 1
    For me it is a syntax error in a dependency newly added. I correct the group/artifact name and all went back to track. At first it was not obvious. – WesternGun Jul 16 '18 at 10:44
  • 1
    I think the step #8 is most important, removing old run configuration solved similar problem for me – Arun Y Mar 12 '19 at 10:06
  • @AndyBrown 's solution fixed it for me. Moving the test class to src/test (form src/main) solved problem. – 8xomaster Sep 12 '19 at 13:37
24

this error can be caused by adding the JUnit library to Modulepath rather than Classpath.

in Eclipse the left most panel "Package Explorer" right click your project go down to properties then go to "Java Build Path" Click on "Classpath" NOT "Modulepath" click "Add Library..." then Junit.

enter image description here

enter image description here

user_number153
  • 492
  • 4
  • 7
1

In my case the error had the same stack trace, (ending with java.lang.Class.forName0(Native Method)) but the error message was different:

java.lang.VerifyError: (class: org/junit/runner/manipulation/Alphanumeric, method: create signature: (Lorg/junit/runner/manipulation/Ordering$Context;)Lorg/junit/runner/manipulation/Ordering;) Wrong return type in function

The cause turned out to be that I had upgraded JUnit from 4.11 to 4.13.1, because GitHub's "dependabot" suggested so, on some public project of mine, and even created merge requests ready for me to accept, and I thought "sure, why not, what could possibly go wrong?" Conclusion: don't trust GitHub's dependabot.

Mike Nakis
  • 56,297
  • 11
  • 110
  • 142
0

Looks like this is a defect in eclipse, please update eclipse or eclipse based editor to fix this issue.

Please refer here for more details - https://bugs.eclipse.org/bugs/show_bug.cgi?id=525844

Abhishek Kumar
  • 435
  • 1
  • 6
  • 16
0

In my case it was just running a Maven update in Eclipse and then it worked again.

Holger
  • 1