JUnit4 Annotations and Ant Workaround

I usually use Maven for Java based projects, so my ant skills are a little rusty. Recently It was whipping up some JUnit negative test cases for someone else’s project that was a mess of ant build files (ugh). I decided to use some of the nifty annotation features found in JUnit 4.8.2 ( for rules and expected exception behavior). Annotations are great for addressing defects in the Java language, and help encapsulation by associating behavior directly with a class. But I digress… Anyway All the tests to check exceptions were thrown under negative test conditions looked good inside of Eclipse, but when I ran the ant test script from the command line, none of tests were being run encfprcomg the annotations, even though the JUnit jar was in the ant classpath! A little research showed that ant’s JUnit test runner doesn’t support annotations. The solution was to modify the suite() method of the AllTests class to wrap the classes under test in a JUnit4TestAdapter, i.e. something like this:

public class AllTests extends TestSuite {
	public static junit.framework.Test suite() {
	        return new JUnit4TestAdapter(TestSomething.class);