After a bit of time away from Java, I recently joined a new team and am trying to promote the use of JUnit and unittesting. I created a separate test directory for containing the unit test source files, and got to the point where all the tests I had created were running great from ant using the junit task and batchtest to match all the source files that matched our naming convention for tests. So far, so good.
Now, the problem is that there already exist many manual test classes which are not really JUnit tests - they are Java classes with main's that are intended to be manually run. The problem is the lead dev wants to keep all these tests in the same directory as the JUnit tests, and unfortunately, they have the same naming convention as the unit tests, so using batchtest won't work any more, since it pulls in all these non JUnit tests and fails when it tries to run them.
I didn't think this was going to be a problem -- in my previous Java dev role (probably using JUnit 3.x) I had used an "AllTests" class that built a TestSuite by searching for appropriate classes in the class path. My thinking that was instead of using batchtest, I could do the same kind of thing to ignore all the classes that are just main's. So, I wrote an AllTests class, and so far, I have gotten VERY close to getting it to work. What really confuses me, is that it works when I "Run as JUnit" in Eclipse, but when I run it from ant, it is claiming there are "No tests found in a.b.c.FooTest". I'm using JUnit 4.4 both with Ant and Eclipse, so I'm mystified at the discrepancy. Any suggestions?
[Edit: I should also mention, the unit tests themselves are all using the JUnit 4 style, i.e. they don't extend TestCase, use the @Test annotation, etc.]
Here's what my TestSuite generator looks like:
[ July 30, 2008: Message edited by: Eric Asberry ] [ July 30, 2008: Message edited by: Eric Asberry ]
Originally posted by Jeanne Boyarsky: Eric, Welcome to JavaRanch!
If the tests are JUnit 4 tests for a class, wouldn't the JUnit4TestAdapter throw an error that no tests are found?
Though it's not clear from the formatting in my post, that first line is actually commented out - it's what I had tried first. I had found the JUnit4TestAdapter wrapper doing a google search. And no, it doesn't throw an error, and actually does what I want it to do when I run it from Eclipse.
I ended up taking a different approach for now. I discovered the ability to specify an "includesfile" in a fileset in Ant, which I didn't know about before. This lets you specify a file to read the patterns from. So, I modified the class above to have a main, and I run it as a java program in the ant task, and it writes out the appropriate patterns to a file. Then I use the batchtest method that I was using originally, but pointing includesfile to the file I just wrote out instead of specifying a pattern in the actual Ant script.
I'm not really happy with it, and its probably going to get rather unwieldy as the number of tests grow, but at least in the short-term, its doing what I need it to.
I still don't understand why the original thing works in Eclipse and not in Ant. I would expect it to either work or not work consistently, so obviously I'm missing something.
BTW, it only works in Eclipse using the JUnit4TestAdapter wrapper ... if I don't use that, it doesn't work in Eclipse either.