4.9 Running a Test Class Directly

4.9.1 Problem

You don't want to invoke one of the JUnit test runners. Instead, you would like to run your test directly.

4.9.2 Solution

Add a main( ) method to your test case. Or, use Ant to run your tests as discussed in Chapter 3.

4.9.3 Discussion

Adding a main( ) method can make a class easier to run. Most IDEs allow you to click on a class and select some sort of "run" option from a popup menu, provided the class has a main( ) method. Here is a sample main( ) method for our TestGame class:

public class TestGame extends TestCase {
    ...
    public static void main(String[] args) {
        junit.textui.TestRunner.run(new TestSuite(TestGame.class));
    }
}

When executed, this method runs the test suite in text mode. Output is sent to the console.

4.9.4 See Also

Recipe 4.3 shows how to run unit tests. Chapter 3 shows how to run tests using Ant.