Making JUnit 4 backwards compatible
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.

January 15th, 2007 at 4:19 am
I used to code my tests with JUnit 3.X, but have switched to TestNG since I found that JUnit 4.X does not support JDK 1.4.X. Not only does TestNG looks more intuitive to me for test configurations, but it also has more features than JUnit, e.g. factories, parametric testing, and run tests in parallel, etc. Once I started using TestNG, I never look back.
September 21st, 2007 at 3:39 pm
Your simple solution helped me .. thanks
September 28th, 2007 at 12:46 pm
Even if I use Ant 1.7 with junit 4 I get the same errors ? Any suggestions ?
How should I be avoiding the No tests found warning message wihtout adding the JUnitJUnit4TestAdapter thing in my test class.
Thanks