4

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.

Community
  • 1
  • 1
Neek
  • 7,181
  • 3
  • 37
  • 47

1 Answers1

7

Well this is embarrassing, but it deserves to be said. I think I just answered my own question.

The answer seems to be one of the tasks attributes, enableTestListenerEvents, described here: http://ant.apache.org/manual/Tasks/junit.html

I had ignored it because the docs say "since Ant 1.8.2 - Ant 1.7.0 to 1.8.1 behave as if this attribute was true by default." and I'm running 1.8.2, but I'd misread it, it's actually saying that in 1.8.2 it's off by default.

So the fix is to change my task in build.xml:

<junit haltonfailure="true" printsummary="on" fork="yes" dir=".">

to

<junit haltonfailure="true" printsummary="on" fork="yes" dir="."
    enableTestListenerEvents="true">

NetBeans now prints up-to-the-moment information in the Test Results pane, including test names, as the tests progress. The whole feedback experience is far richer. Tell your friends :)

Neek
  • 7,181
  • 3
  • 37
  • 47