Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JUnit: NoClassDefFoundError: org/junit/runner/manipulation/Filter

Tags:

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>
like image 295
lucian.marcuta Avatar asked Nov 04 '15 12:11

lucian.marcuta


People also ask

How do I resolve NoClassDefFoundError in JUnit?

To resolve module dependency, we use the module path. However, adding external jars in the module path does not make them available for the class loader. Hence the class loader considers them as missing dependencies and throws the NoClassDefFoundError.

How do I fix JUnit initialization error?

Right click your project in Package Explorer > click Properties go to Java Build Path > Libraries tab click on 'Add Library' button select JUnit click Next.


3 Answers

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.

like image 142
mmwaikar Avatar answered Sep 24 '22 21:09

mmwaikar


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

like image 24
user_number153 Avatar answered Sep 26 '22 21:09

user_number153


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.

like image 1
Mike Nakis Avatar answered Sep 26 '22 21:09

Mike Nakis