How to run junit test code
The example below shows how to run a test class.
If the class TestMethodSetUp contains methods annotated with @org.junit.Test, then to run it, you need to invoke JUnitCore.runClasses(TestMethodSetUp.class) and then analyze the Result objects returned.
If the class TestMethodSetUp contains methods annotated with @org.junit.Test, then to run it, you need to invoke JUnitCore.runClasses(TestMethodSetUp.class) and then analyze the Result objects returned.
package com.techfundaes.junit4Bag; import org.junit.runner.JUnitCore; import org.junit.runner.Result; import org.junit.runner.notification.Failure; public class TestRunner { public static void main(String[] args) { Result result = JUnitCore.runClasses(TestMethodSetUp.class); if(result.getFailures().size() == 0) { System.out.println("All tests successful !!!"); } else { System.out.println("No. of failed test cases="+result.getFailures().size()); for (Failure failure : result.getFailures()) System.out.println(failure.toString()); } } }