Run org.junit.runner.JUnitCore in junit-4.12.jar

Q

How to run org.junit.runner.JUnitCore in junit-4.12.jar?

✍: FYIcenter.com

A

org.junit.runner.JUnitCore is the main class of the JUnit 4.12, which requires hamcrest-core-1.3.jar.

If you run org.junit.runner.JUnitCore without Hamcrest, you will get a NoClassDefFoundError exception:

\fyicenter>java -version
java version "1.8.0_45"

\fyicenter>java -cp .;\local\lib\junit-4.12.jar 
   org.junit.runner.JUnitCore
JUnit version 4.12

Exception in thread "main" java.lang.NoClassDefFoundError: 
org/hamcrest/SelfDescribing
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:760)
at java.security.SecureClassLoader.defineClass
   (SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:467)
at java.net.URLClassLoader.access$100(URLClassLoader.java:73)
at java.net.URLClassLoader$1.run(URLClassLoader.java:368)
at java.net.URLClassLoader$1.run(URLClassLoader.java:362)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:361)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at org.junit.runner.Computer.getSuite(Computer.java:28)
at org.junit.runner.Request.classes(Request.java:75)
at org.junit.runner.JUnitCommandLineParseResult.createRequest
   (JUnitCommandLineParseResult.java:118)
at org.junit.runner.JUnitCore.runMain(JUnitCore.java:77)
at org.junit.runner.JUnitCore.main(JUnitCore.java:36)
...

If you run org.junit.runner.JUnitCore with junit-4.12.jar, you will get OK message:

\fyicenter>java -version
java version "1.8.0_45"

\fyicenter>java -cp .;\local\lib\junit-4.12.jar;
  \local\lib\hamcrest-core-1.3.jar org.junit.runner.JUnitCore
JUnit version 4.12

Time: 0.001

OK (0 tests)

The output shows that 0 tests performed, because no test class is given.

Back to FAQ for JUnit (Java Unit) Testing.

2016-02-24, 2588👍, 0💬