Because JUnit 4’s testing model has fundamentally changed due to the introduction of Java 5 annotations, many people have found that the extensive framework of runners, like Ant’s venerable junit task, don’t work.

For example, running the following JUnit 4 test in Ant (pre 1.7) will yield some interesting results.

import static org.junit.Assert.assertEquals;
import org.junit.Test;

public class SimpleTest {
 @Test
 public void verifySimpleAssert(){
  assertEquals("simple test", "simple test");
 }
}

Using the junit task in Ant yields the following errors:

[junit] Running test.com.acme.SimpleTest
[junit] Tests run: 1, Failures: 1, Errors: 0, Time elapsed: 0.047 sec
[junit] Testsuite: test.com.acme.SimpleTest
[junit] Tests run: 1, Failures: 1, Errors: 0, Time elapsed: 0.047 sec

[junit] Testcase: warning took 0.016 sec
[junit]     FAILED
[junit] No tests found in test.com.acme.SimpleTest
[junit] junit.framework.AssertionFailedError: No tests found in
  test.com.acme.SimpleTest
[junit] Test test.com.acme.SimpleTest FAILED

In order to have pre 1.7 versions of Ant run your JUnit 4 tests, you must retro fit the test case with a suite method that returns an instance of JUnit4TestAdapter like so:

public static junit.framework.Test suite(){
 return new JUnit4TestAdapter(SimpleTest.class);
}

You must fully qualify the return type of Test in this instance because of the similarly named @Test annotation. Accordingly, once this suite method is in place, Ant will happily run JUnit 4 tests.