The Java command-line program executes a main. What if we want to run a test column with a command line? Of course maven can be very convenient to do, the following describes a relatively primitive approach. We're going to use org.junit.runner.JUnitCore here.
1, to be carried out the test class, in order to space, I try to brief
Import Org.junit.test;public class CTest {@Testpublic void Ctest1 () {System.out.println ("Ctest1");}}
2. Write a Main
Import Org.junit.runner.junitcore;import Org.junit.runner.result;import org.junit.runner.notification.Failure; public class Junitrunner {/** * @param args */public static void main (string[] args) {result result = Junitcore.runclasses (Ctest.class); for (Failure failure:result.getFailures ()) {System.out.println (failure.tostring ());} if (result.wassuccessful ()) {System.out.println ("All test columns executed successfully");}}}
3, implementation. The JUnit dependency jar is required for execution, but Eclipse has the JUnit jar. To facilitate execution, I put the dependency jar junit-4.11.jar/
Hamcrest-core-1.3.jar Copy the directory to the Java file.
Compilation: Javac-encoding UTF-8-classpath.;. /junit-4.11.jar;. /hamcrest-core-1.3.jar Ctest.java Junitrunner.java
Execution: Java-classpath.;. /junit-4.11.jar;. /hamcrest-core-1.3.jar Junitrunner
The output is as follows:
Ctest1 all test columns executed successfully
JUnit command line execution