30

I used Intellij Idea 12 Community edition. I am trying to create test case for my class by creating test case. When i run my test case it says

java.lang.NoClassDefFoundError: org/hamcrest/SelfDescribing
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClassCond(ClassLoader.java:631)
    at java.lang.ClassLoader.defineClass(ClassLoader.java:615)
    at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:141)
    at java.net.URLClassLoader.defineClass(URLClassLoader.java:283)
    at java.net.URLClassLoader.access$000(URLClassLoader.java:58)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:197)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
    at org.junit.internal.builders.JUnit4Builder.runnerForClass(JUnit4Builder.java:10)
    at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59)
    at org.junit.internal.builders.AllDefaultPossibilitiesBuilder.runnerForClass(AllDefaultPossibilitiesBuilder.java:26)
    at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59)
    at org.junit.internal.requests.ClassRequest.getRunner(ClassRequest.java:26)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:44)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:195)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:63)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)
Caused by: java.lang.ClassNotFoundException: org.hamcrest.SelfDescribing
    at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
    ... 25 more

How would i run test case in Intellij. I included the junit4.11 jar file to my module

Pete B.
  • 3,188
  • 6
  • 25
  • 38
jackyesind
  • 3,343
  • 14
  • 47
  • 74
  • 1
    Changing my JUNIT 4.12 to 4.8 worked well. Also add the jar to your class path in the project structure of your IDE. After adding, compile the class you are testing on, and then try running the test file. I hope it works well then. – Dhiraj Gandhi Sep 25 '17 at 13:38

4 Answers4

54

Did you include the hamcrest-core-1.3.jar file in your classpath? If not included means include that jar and try once again.

Ben
  • 54,723
  • 49
  • 178
  • 224
muthu
  • 5,381
  • 2
  • 33
  • 41
13

add junit.jar to your project dependence. you may also need to add hamcrest.jar in addition.

Open File->Project Structure,Click Modules->Dependences,add junit.jar.

bowman han
  • 1,097
  • 15
  • 25
4

Latest version of hamcrest-all is available at: https://search.maven.org/search?q=a:hamcrest-all

Download this version (as opposed to the hamcrest-core) and that should do it.

1

If you come across this (and a few other threads) on this error and adding the jar to dependencies doesn't work, you will need to add to the general java classpath.

I encountered this error in OSX where maven would download the dependency, but not find it when in fork mode.

Adding all the hamcrest jars to Library/Java/Extensions finally fixed the problem when nothing else would.

Brenn
  • 1,364
  • 1
  • 12
  • 22
  • This worked for me as well, I don't know why adding the Maven dependencies wouldn't though. – Isaac May 05 '16 at 10:03