Using JUnit 4, my ant build script that invokes junit with the task always reports the test class names as "unknown". Is there a way of fixing this that does not involve deriving from TestCase?
I declare my test methods with the @Test annotation, e.g.:
SomeTest.java:
public class SomeTest {
@Test
public void doSomething() { .. }
}
and collect them together in a TestSuite:
AllTests.java:
@RunWith(Suite.class)
@Suite.SuiteClasses( {
SomeTest.class
})
public class AllTests {
}
I then invoke the ant task via NetBeans, which has nice integrated support for printing results of a JUnit run. Everything seems find except the test names are always "unknown", e.g.:
6 tests passed, 2 tests failed, 2 tests caused an error (8.4s)
com.mystuff.AllTests FAILED
Unknown passed (0.0s)
Unknown passed (0.0s)
Unknown passed (0.0s)
Unknown passed (0.0s)
Unknown passed (0.0s)
Unknown passed (0.0s)
Unknown FAILED (0.0s)
Unknown caused an ERROR: at org.hibernate.somethingorother.java:1234 (0.0s)
..etc..
The tests operate correctly, and I can usually figure out the point of failure from the stack trace that's logged, but everything being reported as 'Unknown' is simply annoying and obtuse.
Having read up on a few SO posts, I discovered there are two ways to set your tests up, the old JUnit 3 way of deriving from TestCase, and the new Junit 4 way of using @Test annotations.
Not able to execute tests with @Test annotation when my test extends TestCase(Junit) in Eclipse
JUnit confusion: use 'extends TestCase' or '@Test'?
And this post mentions "In JUnit 4 the test classes no longer extend a common framework class. So there's no inherited method getName any more" How to obtain test case name in JUnit 4 at runtime?
I tried extending my test case from TestCase, and this did make the name be reported correctly, but this single change screwed up some of its lifecycle and the tests failed in bizzarre ways. My understanding is that when the JUnit runner sees your tests class is derived from TestCase is runs it as a JUnit 3 test case and would therefore ignore all my @Before, @After etc. annotations. I don't see the point in stepping back into JUnit 3 test mechanisms.