Monthly Archives: April 2016

JUnit test runners

In this topic, let us learn about various test runners available to run JUnit tests.

Using IDE

All popular IDEs like Eclipse, IntelliJ IDEA, Net beans provide plug-ins to run JUnit tests from within IDE.

Using command line

We can also run the tests from command line using below syntax.

java org.junit.runner.JUnitCore MyTestClass1 MyTestClass2 MyTestClass3

Using runClasses method of JUnitCore class

We can also run JUnit tests using JUnitCore class. This class provides a method called as runClasses to which we can pass list of classes to be run. In below example, tests from 2 test classes will be run – OrderedTests and HamcrestTests.

package runners;

import hamcrest.TestHamcrest;
import ordered_tests.OrderedTests;
import org.junit.runner.JUnitCore;

/**
 * Created by Sagar on 30-04-2016.
 */
public class JunitRunners {

    public static void main(String [] args){
        JUnitCore.runClasses(OrderedTests.class, HamcrestTests.class);
    }
}

Using custom Runners

We can also have custom runners as well created using @RunWith annotation. For example, popular BDD framework cucumber uses custom runner to run JUnit tests using @RunWith annotation.